Maven不会编译Selenium依赖项

时间:2016-11-15 19:51:30

标签: java maven selenium intellij-idea

我使用JavaFX和Selenium库在IntelliJ中构建了一个项目。在IDE内部运行时,项目按预期运行。当使用mvn clean;mvn compile编译,而不是通过它新创建的.jar文件执行时,程序的JavaFX部分可以工作,但找不到Selenium库。这是我的pom.xml文件:

<?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>

    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                            <mainClass>Main</mainClass>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${com.toasttab.Epsonaut}/lib</outputDirectory>
                            <includeScope>compile</includeScope>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>



<groupId>com.jagdpanzer.epsonaut</groupId>
    <artifactId>Epsonaut</artifactId>
    <version>1.0</version>

    <dependencies>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.43.0</version>
    </dependency>

    </dependencies>


</project>

如何成功构建此项目,以便在pom中定义的Selenium依赖项在IDE之外工作?

编辑:我的MANIFEST.MF文件位于下方。

 Manifest-Version: 1.0
 Implementation-Title: Epsonaut
 Implementation-Version: 1.0-SNAPSHOT
 Archiver-Version: Plexus Archiver
 Built-By: dougdemars
 Implementation-Vendor-Id: com.jadgpanzer.epsonaut
 Class-Path: lib/selenium-java-2.43.0.jar lib/selenium-chrome-driver-2.
  43.0.jar lib/selenium-remote-driver-2.43.0.jar lib/cglib-nodep-2.1_3.
  jar lib/json-20080701.jar lib/selenium-api-2.43.0.jar lib/guava-15.0.
  jar lib/selenium-htmlunit-driver-2.43.0.jar lib/htmlunit-2.15.jar lib
  /xalan-2.7.1.jar lib/serializer-2.7.1.jar lib/commons-collections-3.2
  .1.jar lib/commons-lang3-3.3.2.jar lib/httpmime-4.3.3.jar lib/commons
  -codec-1.9.jar lib/htmlunit-core-js-2.15.jar lib/xercesImpl-2.11.0.ja
  r lib/xml-apis-1.4.01.jar lib/nekohtml-1.9.21.jar lib/cssparser-0.9.1
  4.jar lib/sac-1.3.jar lib/commons-logging-1.1.3.jar lib/jetty-websock
  et-8.1.15.v20140411.jar lib/jetty-util-8.1.15.v20140411.jar lib/jetty
  -io-8.1.15.v20140411.jar lib/jetty-http-8.1.15.v20140411.jar lib/http
  client-4.3.4.jar lib/httpcore-4.3.2.jar lib/selenium-firefox-driver-2
  .43.0.jar lib/commons-io-2.4.jar lib/commons-exec-1.1.jar lib/seleniu
  m-ie-driver-2.43.0.jar lib/jna-3.4.0.jar lib/platform-3.4.0.jar lib/s
  elenium-safari-driver-2.43.0.jar lib/selenium-support-2.43.0.jar lib/
  webbit-0.4.15.jar lib/netty-3.5.5.Final.jar
 Created-By: Apache Maven 3.3.9
 Build-Jdk: 1.8.0_101
 Main-Class: Main

2 个答案:

答案 0 :(得分:2)

问题是,当您的依赖项被复制到lib/时,您的jar的类路径指向相对目录${com.toasttab.Epsonaut}/lib

确保<outputDirectory><classpathPrefix>具有相同的价值。

如果您希望能够从任何路径执行jar,我建议将maven-assembly-pluginsingle目标一起使用。

<build>
  <plugins>
    <plugin>
      <artifactId>maven-assembly-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>your.package.MainClass</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </plugin>
  </plugins>
</build>

并使用mvn clean compile assembly:single运行它。

答案 1 :(得分:1)

通过ZIP打开最终jar文件,检查MANIFEST.MF文件Class-Path:值。
必须可从当前目录访问其中的所有路径。
当前意味着您调用java来执行该jar文件。