是否可以包含我在编译.jar时使用的模块?

时间:2014-05-20 23:39:20

标签: java selenium-webdriver selenium-chromedriver

我对Java的了解非常少,主要来自Python背景,我想知道是否可以包含我用于.jar的模块?

例如,我的程序使用Selenium Webdriver和Selenium Chromedriver,我可以制作它以便我的用户不需要安装这些吗?

如果之前有人问过这个问题,谢谢你,对不起!

2 个答案:

答案 0 :(得分:0)

您可以在jar中包含资源 请参阅使用mavenAnt

打包

这适用于 Selenium 依赖关系,但 ChromeDriver 是可执行文件,因此您必须在使用之前将其解压缩,请参阅此示例:{{3 }}

答案 1 :(得分:0)

如果你正在使用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的Assembly插件主要是为了允许用户将项目输出及其依赖项,模块,站点文档和其他文件聚合到一个可分发的存档中 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,则此解决方案可能不适合您。