Java 使用maven程序集插件过滤maven中的可运行fat.jar
我目前正在将一个小项目转移到Maven,我遇到了一些麻烦 设置如下所示 其中,我的资源文件夹中使用了一个设置文件,用于在eclipse中运行应用程序。但是当我创建一个胖jar时,设置文件被打包在jar的旁边。我想过滤设置文件,使其位于jar之外。我知道这会使文件变得脆弱,但这是出于设计,并且应用程序仍然可以从与jar相同的文件夹中使用它。我通过eclipse可运行jar和Ant文件实现了这一点。但我似乎无法让它与maven一起工作。你的任何见解都会非常有用 她的例子是我的插件和过滤器文件。关于这件事的任何想法都会很棒,因为这已经让我发疯了Java 使用maven程序集插件过滤maven中的可运行fat.jar,java,eclipse,maven,maven-assembly-plugin,Java,Eclipse,Maven,Maven Assembly Plugin,我目前正在将一个小项目转移到Maven,我遇到了一些麻烦 设置如下所示 其中,我的资源文件夹中使用了一个设置文件,用于在eclipse中运行应用程序。但是当我创建一个胖jar时,设置文件被打包在jar的旁边。我想过滤设置文件,使其位于jar之外。我知道这会使文件变得脆弱,但这是出于设计,并且应用程序仍然可以从与jar相同的文件夹中使用它。我通过eclipse可运行jar和Ant文件实现了这一点。但我似乎无法让它与maven一起工作。你的任何见解都会非常有用 她的例子是我的插件和过滤器文件。关于这
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<filters>
<filter>src/assembly/filter.properties</filter>
</filters>
<archive>
<manifest>
<mainClass>ie.business.project.artifact</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
还有我的过滤器
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0
http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>distribution</id>
<formats>
<format>jar</format>
</formats>
<files>
<file>
<source>settings.ini</source>
<outputDirectory>/</outputDirectory>
<filtered>true</filtered>
</file>
</files>
</assembly>
您似乎没有使用指向问题中程序集的程序集插件。如果您查看正在使用的插件配置:
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
实际上,您应该指向部件文件的相对路径,例如:
<configuration>
<descriptors>
<descriptor>src/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
现在,应该更改用于创建胖jar的程序集文件,以创建胖jar并排除一些文件。这个例子只是给你一个想法:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<fileSets>
<fileSet>
<directory>${basedir}</directory>
<excludes>
<exclude>settings.ini</exclude>
</excludes>
</fileSet>
</fileSets>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
所以我有点累了。如果除了使用maven assembly之外,我还在jar插件中定义了一个排除,那么设置文件就不会出现在fat jar中。我不确定这是否是正确的方法,但它已经奏效了 请让我知道是否有更好的方法来做到这一点 我在我的pom中添加了以下插件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<excludes>
<exclude>setting.ini</exclude>
</excludes>
</configuration>
</plugin>
您知道filter.properties只能包含用于替换值的属性定义吗?那么我该如何从jar中过滤文件呢?嗨,吉尔,非常感谢你的例子,我还没有意识到id是以这种方式绑定的。你的例子帮了我很多忙,但我还是没能实现我想要的,只是祝贺你让这项工作起作用。我对你的回答投了赞成票。