我想创建一个可执行 jar(其中包含我的代码的所有 *.class)。 但是,我不希望 Jar 包含编译期间位于我的 src/main/resources 路径中的资源。
我的项目层次结构是:
project
-src
-main
-resources
-resources_setting.xml
-target
-classes
-resources_setting.xml
我希望我的 jar 只包含 main 类和依赖项,而不包含 target\classes 或内部资源内的资源。
我该怎么做?
我正在使用 maven-assemble-plugin,如下所示:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>cqm.qa.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
最佳答案
出于捆绑目的,我通常使用maven-shade-plugin
,设置如下所述。它的工作方式与程序集插件相同。
<profile>
<id>generate-shaded-jar</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>cqm.qa.Main</Main-Class>
<Class-Path>.</Class-Path>
</manifestEntries>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>log4j.properties</exclude>
<exclude>details.properties</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
<configuration>
<finalName>cqm-full</finalName>
</configuration>
</plugin>
</plugins>
</build>
</profile>
在上面的配置中,我从最终的 jar 中排除了 log4j.properties
和 details.properties
,其依赖项名为 cqm-full.jar
更新
使用mvn install -Pgenerate-shaded-jar
调用配置文件
现在,src/main/resources
中的资源文件将不会添加到 cqm-full.jar
中。如果在没有配置文件mvn clean install
的情况下调用,您仍然可以查看jar中的资源
关于java - 没有目标\类的 Maven jar-with-dependencies(构建 Artifact ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32296811/