【问题标题】:IntelliJ Maven Selenium Build jarIntelliJ Maven Selenium 构建 jar
【发布时间】:2013-03-01 13:14:47
【问题描述】:

我正在使用 IntelliJ 12、Java 7、Selenium 2.31.0 和 Maven。我可以很好地从 IDE 中运行我的测试,但是我在尝试创建 jar 文件时遇到了问题。我可以创建 jar 文件,双击 mvn clean 然后双击安装。一切都很好,并且创建了 jar。当我通过命令行运行jar时会出现问题。

java -jar xyz-selenium-test-1.0.jar

返回:线程“main”中的异常 java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver

我已将 selenium-server-standalone-2.31.0.jar 添加为项目设置中的库和项目设置中的依赖模块。我的 pom 文件中一定遗漏了一些东西,但我只是不知道它是什么。我也附上了我的 pom 文件。

<?xml version="1.0" encoding="UTF-8"?>
<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.xyz.selenium.test</groupId>
<artifactId>xyz-selenium-test</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>xyz-selenium-test</name>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.31.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>2.31.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-htmlunit-driver</artifactId>
<version>2.31.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>selenium-jar</groupId>
<artifactId>selenium-server-standalone-2.31.0.jar</artifactId>
<version>2.31.0</version>
<scope>system</scope>
<systemPath>/usr/local/selenium/selenium-server-standalone-2.31.0.jar</systemPath>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>2.31.0</version>
<exclusions>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<index>true</index>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.setup.test.Setup</mainClass>
</manifest>
<manifestEntries>
<mode>development</mode>
<url>${project.url}</url>
<key>value</key>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>

【问题讨论】:

    标签: java maven selenium jar intellij-idea


    【解决方案1】:

    运行程序时,必须将依赖项添加到类路径中:

    java -cp "selenium-java.jar:..." -jar xyz-selenium-test-1.0.jar
    

    Maven 还允许您在类路径中使用run a Main class with all the required dependencies(请参阅上一个链接中的选项 1)。

    否则,您可以创建一个Jar with dependencies,其中将包含运行代码所需的一切:

    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>com.setup.test.Setup</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
      <executions>
        <execution>
          <id>make-assembly</id>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    

    然后运行

    java -jar xyz-selenium-test-jar-with-dependencies-1.0.jar
    

    【讨论】:

    • 谢谢!这正是我所需要的!我没有足够的声誉来提高这一点,但非常感谢。
    【解决方案2】:

    你也可以使用 OneJar 插件

     <plugin>
                <groupId>org.dstovall</groupId>
                <artifactId>onejar-maven-plugin</artifactId>
                <version>1.4.4</version>
                <executions>
                    <execution>
                        <configuration>
                            <onejarVersion>0.97</onejarVersion>
                            <attachToBuild>true</attachToBuild>
                            <!-- Optional, default is "onejar" -->
                            <classifier>onejar</classifier>
                        </configuration>
                        <goals>
                            <goal>one-jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-05
      • 2022-01-16
      • 2015-07-24
      • 2013-09-07
      • 2011-10-05
      • 1970-01-01
      相关资源
      最近更新 更多