【问题标题】:Error when executing a maven jar执行 maven jar 时出错
【发布时间】:2017-08-21 17:24:14
【问题描述】:

我想执行一个 maven jar 时遇到问题。我不明白为什么。我认为 maven 不会导出依赖项,但我不明白为什么。

我的错误:

The error screenshot

还有我的 POM.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cgi.nouguierc</groupId>
<artifactId>WD-Automation</artifactId>
<version>1.0.0-SNAPSHOT</version>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>selenium.MainJar</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
               <execution>
                     <id>copy-dependencies</id>
                     <phase>prepare-package</phase>
                     <goals>
                        <goal>copy-dependencies</goal>
                     </goals>
                     <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                     </configuration>
               </execution>
            </executions>
        </plugin>
    </plugins>
</build>
<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.3.1</version>
    </dependency>
    <dependency>
        <groupId>com.relevantcodes</groupId>
        <artifactId>extentreports</artifactId>
        <version>2.41.2</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
</dependencies>
</project>

你能帮帮我吗?

【问题讨论】:

    标签: eclipse maven selenium junit jar


    【解决方案1】:

    转到您的项目 =>右键单击 => 运行方式 => Maven 构建...(有 3 个尾随点) =>在目标中写入 clean install

    它将在目标文件夹中创建两个 jars。

    a) WD-Automation-1.0.0-SNAPSHOT.jar

    b) WD-Automation-1.0.0-SNAPSHOT-jar-with-dependencies.jar

    第二个 jar 是可运行的。将它放在你的 chrome 驱动程序所在的位置并转到 jar 的位置并运行它,它会工作。

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.cgi.nouguierc</groupId>
        <artifactId>WD-Automation</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <build>
            <sourceDirectory>src</sourceDirectory>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.5.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>com.stack.JarCreation.MainJar</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>attached</goal>
                            </goals>
                            <phase>package</phase>
                            <configuration>
                                <descriptorRefs>
                                    <descriptorRef>jar-with-dependencies</descriptorRef>
                                </descriptorRefs>
                                <archive>
                                    <manifest>
                                        <mainClass>com.stack.JarCreation.MainJar</mainClass>
                                    </manifest>
                                </archive>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    
            </plugins>
        </build>
        <dependencies>
            <dependency>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-java</artifactId>
                <version>3.3.1</version>
            </dependency>
            <dependency>
               <groupId>com.relevantcodes</groupId>
               <artifactId>extentreports</artifactId>
               <version>2.41.2</version>
           </dependency>
           <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.11</version>
          </dependency>
     </dependencies>
    

    【讨论】:

      【解决方案2】:

      我认为你使用了错误的 jar 文件。

      生成的具有依赖关系的 jar 文件应位于项目的“target/lib”中 (${project.build.directory}/lib),而不是 maven 存储库。

      【讨论】:

        猜你喜欢
        • 2010-12-22
        • 1970-01-01
        • 2012-01-09
        • 1970-01-01
        • 1970-01-01
        • 2017-06-27
        • 2015-02-09
        • 1970-01-01
        • 2018-11-09
        相关资源
        最近更新 更多