如果您使用的是 maven,那么您可以使用 maven-shade-plugin 并且您可以配置您希望在项目构建 jar 中包含和排除库中的内容
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>junit:junit</artifact>
<includes>
<include>junit/framework/**</include>
<include>org/junit/**</include>
</includes>
<excludes>
<exclude>org/junit/experimental/**</exclude>
<exclude>org/junit/runners/**</exclude>
</excludes>
</filter>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
欲了解更多信息,请参阅:
http://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html
您也可以使用 maven-assembly-plugin
用于 Maven 的程序集插件主要是为了允许用户将项目输出及其依赖项、模块、站点文档和其他文件聚合到一个可分发的存档中 more
我查看了你的 pom 文件。因此,我发布一个工作示例 pom 文件,该文件是根据您在 pastebin 中提供的文件制作的。
<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.selenium.pro</groupId>
<artifactId>packjar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>packing library with jar</name>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.41.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<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>
</plugins>
</build>
<profiles>
<profile>
<id>prod</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>junit:junit</artifact>
<includes>
<include>junit/framework/**</include>
<include>org/junit/**</include>
</includes>
<excludes>
<exclude>org/junit/experimental/**</exclude>
<exclude>org/junit/runners/**</exclude>
</excludes>
</filter>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
通常对于您的开发,您不需要打包库,但您可以验证它。当您必须将应用程序构建到客户端时,您可以运行
mvn clean package -Pprod //profile prod
这是构建日志的一部分
[INFO] --- maven-shade-plugin:2.3:shade (default) @ packjar ---
Downloading: http://repo.maven.apache.org/maven2/org/ow2/asm/asm/5.0.2/asm-5.0.2.jar
Downloaded: http://repo.maven.apache.org/maven2/org/ow2/asm/asm/5.0.2/asm-5.0.2.jar (52 KB at 9.0 KB/sec)
[INFO] Including org.seleniumhq.selenium:selenium-java:jar:2.41.0 in the shaded jar.
[INFO] Including org.seleniumhq.selenium:selenium-chrome-driver:jar:2.41.0 in the shaded jar.
[INFO] Including org.seleniumhq.selenium:selenium-remote-driver:jar:2.41.0 in the shaded jar.
[INFO] Including cglib:cglib-nodep:jar:2.1_3 in the shaded jar.
[INFO] Including org.json:json:jar:20080701 in the shaded jar.
[INFO] Including com.google.guava:guava:jar:15.0 in the shaded jar.
[INFO] Including org.seleniumhq.selenium:selenium-htmlunit-driver:jar:2.41.0 in the shaded jar.
[INFO] Including net.sourceforge.htmlunit:htmlunit:jar:2.13 in the shaded jar.
[INFO] Including xalan:xalan:jar:2.7.1 in the shaded jar.
[INFO] Including xalan:serializer:jar:2.7.1 in the shaded jar.
[INFO] Including commons-collections:commons-collections:jar:3.2.1 in the shaded jar.
[INFO] Including org.apache.commons:commons-lang3:jar:3.1 in the shaded jar.
[INFO] Including org.apache.httpcomponents:httpmime:jar:4.3.1 in the shaded jar.
[INFO] Including commons-codec:commons-codec:jar:1.8 in the shaded jar.
[INFO] Including net.sourceforge.htmlunit:htmlunit-core-js:jar:2.13 in the shaded jar.
[INFO] Including xerces:xercesImpl:jar:2.11.0 in the shaded jar.
[INFO] Including xml-apis:xml-apis:jar:1.4.01 in the shaded jar.
注意:如果您使用签名的库 jar,那么此解决方案可能不适合您。