2013-03-13 105 views
0

我正在使用IntelliJ 12,Java 7,Selenium 2.31.0和Maven。我可以從IDE運行我的測試,但我遇到了嘗試創建jar文件的問題。我可以創建jar文件雙擊mvn clean然後雙擊安裝。一切都很好,罐子被創建。當我通過命令行運行jar時會出現問題。IntelliJ Maven Selenium Build jar

Java的罐子XYZ硒 - 測試 - 1.0.jar

返回:異常在線程 「主要」 java.lang.NoClassDefFoundError:組織/ openqa /硒/ webdriver的

我已經加入硒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> 

回答

4

你有你的依賴,當你運行你的程序添加到類路徑:

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 
+0

謝謝!那就是我需要的!我沒有足夠的聲望來解決這個問題,但感謝一堆。 – cbohannon 2013-03-13 18:55:07

0

您還可以使用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>