Maven创建一个包含依赖项的可执行jar文件

时间:2020-04-28 22:28:39

标签: maven maven-assembly-plugin

我正在尝试创建一个包含所需依赖项的可执行jar文件。我已经成功了,但是依赖关系在我的jar中解压了。我想创建一个版本,在该版本中,依赖项仍打包在其罐内lib文件夹中的追溯罐中,并在MANIFEST.MF类路径中引用。

这是我目前拥有的:

...
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.2.0</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <mainClass>com.example.Main</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>
...

我尝试在没有运气的情况下研究maven-assembly-plugin的选项,还尝试将maven-dependency-pluginmaven-jar-plugin一起使用,如下所示:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>false</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.2.0</version>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
                <mainClass>com.example.Main</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

maven的输出看起来很有希望,但可惜没有高兴:

[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.example:sample >-------------------------
[INFO] Building sample 2.0.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ sample ---
[INFO] Deleting /Users/me/sample/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ sample ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ sample ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /Users/me/sample/target/classes
[INFO] /Users/me/sample/src/main/java/com/example/Main.java: /Users/me/sample/src/main/java/com/example/Main.java uses or overrides a deprecated API.
[INFO] /Users/me/sample/src/main/java/com/example/Main.java: Recompile with -Xlint:deprecation for details.
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ sample ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/me/sample/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ sample ---
[INFO] Changes detected - recompiling the module!
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ sample ---
[INFO]
[INFO] --- maven-dependency-plugin:2.8:copy-dependencies (copy-dependencies) @ sample ---
[INFO] Copying aws-java-sdk-kms-1.11.252.jar to /Users/me/sample/target/lib/aws-java-sdk-kms-1.11.252.jar
[INFO] Copying log4j-1.2.17.jar to /Users/me/sample/target/lib/log4j-1.2.17.jar
...
[INFO] Copying commons-io-2.6.jar to /Users/me/sample/target/lib/commons-io-2.6.jar
[INFO]
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ sample ---
[INFO] Building jar: /Users/me/sample/target/sample-2.0.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.928 s
[INFO] Finished at: 2020-04-28T18:16:02-04:00
[INFO] ------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

» unzip -l /Users/me/sample/target/sample-2.0.0.jar
Archive:  /Users/me/sample/target/sample-2.0.0.jar
  Length      Date    Time    Name
---------  ---------- -----   ----
      601  04-28-2020 18:16   META-INF/MANIFEST.MF
        0  04-28-2020 18:16   META-INF/
        0  04-28-2020 18:16   com/
        0  04-28-2020 18:16   com/example/
        0  04-28-2020 18:16   com/example/util/
        0  04-28-2020 18:16   META-INF/maven/
        0  04-28-2020 18:16   META-INF/maven/com.example/
        0  04-28-2020 18:16   META-INF/maven/com.example/sample/
      266  04-28-2020 18:16   Example.properties
      809  04-28-2020 18:16   log4j.xml
     2289  04-28-2020 18:16   com/example/util/AppProperties.class
    13735  04-28-2020 18:16   com/example/Example.class
     3686  04-28-2020 18:15   META-INF/maven/com.example/sample/pom.xml
       52  04-28-2020 18:16   META-INF/maven/com.example/sample/pom.properties
---------                     -------
    21438                     14 files

0 个答案:

没有答案