【问题标题】:Is it possible to include modules I used when compiling a .jar?是否可以包含我在编译 .jar 时使用的模块?
【发布时间】:2014-05-20 23:39:20
【问题描述】:

我的 Java 知识非常少,主要来自 Python 背景,我想知道是否可以将我在 .jar 中使用的模块包含在内?

例如,我的程序使用 Selenium Webdriver 和 Selenium Chromedriver,是否可以让我的用户不需要安装它们?

谢谢你,如果之前有人问过这个问题,我们很抱歉!

【问题讨论】:

  • 你是否使用maven之类的工具来构建项目
  • my user 是什么意思?您的用户处于哪个位置? API 用户还是程序用户?

标签: java selenium-webdriver selenium-chromedriver


【解决方案1】:

如果您使用的是 maven,那么您可以使用 ma​​ven-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

您也可以使用 ma​​ven-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,那么此解决方案可能不适合您。

【讨论】:

  • 我已经在使用 maven,如果我的依赖项下有一个模块,它会包含它吗?例如,到目前为止,这是我的 pom.xml。 pastebin.com/nykuA9HB 谢谢!
  • 我没有发现您正在使用任何插件和配置来容纳构建 jar 中的库类。
  • 如果对您的问题有意义,您应该接受答案
【解决方案2】:

您可以在 jar 中包含资源 使用mavenAnt查看包装

这将适用于 Selenium 依赖项,但 ChromeDriver 是一个可执行文件,因此您必须在使用它之前将其解压缩,请参见此处的示例:Run *.exe file from inside JAR

【讨论】:

    猜你喜欢
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-27
    相关资源
    最近更新 更多