如何从一个 Maven 项目创建两个具有不同资源文件的 jar
how to create two jars from one Maven project but with different resources files
我有一个 Maven 项目,在 src/main/resources 中有多个文件夹,我想生成两个 Jar,一个包含 src/main/resources/folder1/all 属性,另一个包含 src/main/resources /folder2/所有属性。
他们是实现这一目标的方法吗?如果没有,实现我的目标的最简单方法是什么?
来自Introduction to Build Profiles
Profiles can be activated in the Maven settings, via the section. This section takes a list of elements, each containing a profile-id inside.
<settings>
...
<activeProfiles>
<activeProfile>profile-1</activeProfile>
</activeProfiles>
...
</settings>
Profiles listed in the tag would be activated by default every time a project use it.
Profiles can be automatically triggered based on the detected state of the build environment. These triggers are specified via an section in the profile itself. Currently, this detection is limited to prefix-matching of the JDK version, the presence of a system property or the value of a system property.
这允许根据目标环境创建不同的包内容。
如果我对你的问题理解正确,那么你可以按以下方式使用“Maven Assembly 插件”和“The Assembly Descriptor” :
首先,如果您想同时构建 2 个 jar 文件,则不能使用 profile。
因此,我的建议是从 jar 中排除配置文件,然后使用 Maven Assembly 插件创建包含这些文件夹的不同 zip 文件。
例如,在您的情况下,您应该有 2 个文件描述符,如下所示:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>folder1</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/java/descriptors/folder1.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
和folder1.xml
包含:
<assembly>
<id>folder1</id>
<formats>
<format>zip</format>
</formats>
<files>
<file>
<source>
target/${project.artifactId}-${project.version}-yourJar.jar
</source>
<outputDirectory>/</outputDirectory>
</file>
</files>
<fileSets>
<fileSet>
<directory>${project.basedir}/src/main/resources/folder1</directory>
<includes>
<include>*</include>
</includes>
<outputDirectory>/config</outputDirectory>
</fileSet>
</fileSets>
</assembly>
对于“folder2”,你可以用同样的方法。
还可以使用此插件从 jar 中排除一些配置文件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<configuration>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>package.path.to.your.main.class.MainClass</mainClass>
</manifest>
<manifestEntries>
<Class-Path>conf/</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
使用maven-resources-plugin如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/conf</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
我有一个 Maven 项目,在 src/main/resources 中有多个文件夹,我想生成两个 Jar,一个包含 src/main/resources/folder1/all 属性,另一个包含 src/main/resources /folder2/所有属性。
他们是实现这一目标的方法吗?如果没有,实现我的目标的最简单方法是什么?
来自Introduction to Build Profiles
Profiles can be activated in the Maven settings, via the section. This section takes a list of elements, each containing a profile-id inside.
<settings>
...
<activeProfiles>
<activeProfile>profile-1</activeProfile>
</activeProfiles>
...
</settings>
Profiles listed in the tag would be activated by default every time a project use it. Profiles can be automatically triggered based on the detected state of the build environment. These triggers are specified via an section in the profile itself. Currently, this detection is limited to prefix-matching of the JDK version, the presence of a system property or the value of a system property.
这允许根据目标环境创建不同的包内容。
如果我对你的问题理解正确,那么你可以按以下方式使用“Maven Assembly 插件”和“The Assembly Descriptor” : 首先,如果您想同时构建 2 个 jar 文件,则不能使用 profile。 因此,我的建议是从 jar 中排除配置文件,然后使用 Maven Assembly 插件创建包含这些文件夹的不同 zip 文件。
例如,在您的情况下,您应该有 2 个文件描述符,如下所示:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>folder1</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/java/descriptors/folder1.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
和folder1.xml
包含:
<assembly>
<id>folder1</id>
<formats>
<format>zip</format>
</formats>
<files>
<file>
<source>
target/${project.artifactId}-${project.version}-yourJar.jar
</source>
<outputDirectory>/</outputDirectory>
</file>
</files>
<fileSets>
<fileSet>
<directory>${project.basedir}/src/main/resources/folder1</directory>
<includes>
<include>*</include>
</includes>
<outputDirectory>/config</outputDirectory>
</fileSet>
</fileSets>
</assembly>
对于“folder2”,你可以用同样的方法。
还可以使用此插件从 jar 中排除一些配置文件:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3</version>
<configuration>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>package.path.to.your.main.class.MainClass</mainClass>
</manifest>
<manifestEntries>
<Class-Path>conf/</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
使用maven-resources-plugin如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/conf</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>