我想创建我的 Maven 应用程序的 JAR 文件。因此,为了捆绑依赖项,我使用了 maven-assemble-plugin。但我希望依赖项应该放在 lib/文件夹中,而不是其他地方。我当前的 pom.xml 插件标签是
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
请提出建议。也可以使用 maven jar 插件来实现相同的目的。
在 POM 中进行上述更改后,依赖项会在 jar 中生成,该 jar 生成的后缀为“jar-with-dependencies”。但是依赖的 jar 是分散的,我希望所有这些依赖的 jar 都成为一个文件夹 lib 的一部分,类似于 war 文件的 WEB-INF/lib 。我如何实现这一目标?
最佳答案
您可以使用自定义程序集描述符(xml 文件)来实现此目的,如下所示:
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>dist</id>
<formats>
<format>dir</format>
</formats>
<fileSets>
<fileSet>
<directory>target</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>your-application.jar</include>
</includes>
</fileSet>
</fileSets>
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
</dependencySet>
</dependencySets>
</assembly>
您必须指向 Maven 程序集插件配置中的程序集描述符:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptors>
<descriptor>assemble.xml</descriptor>
</descriptors>
</configuration>
...
</plugin>
希望有帮助
关于java - POM 中指定的依赖项未在正确目录中生成的 jar 中解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26359911/