[英]how to create two jars from one Maven project but with different resources files
I have a single maven project that has multiple folders in src/main/resources, I want to generate two Jars, one include src/main/resources/folder1/all properties and the other include src/main/resources/folder2/all properties.我有一个 maven 项目,它在 src/main/resources 中有多个文件夹,我想生成两个 Jars,一个包括 src/main/resources/folder1/all 属性,另一个包括 src/main/resources/folder2/all 属性.
Is their a way to achive this?他们是实现这一目标的方法吗? If not, what is the simplest way to achive my goal?
如果没有,实现我的目标的最简单方法是什么?
From Introduction to Build Profiles从介绍到构建配置文件
Profiles can be activated in the Maven settings, via the section.
配置文件可以在 Maven 设置中激活,通过该部分。 This section takes a list of elements, each containing a profile-id inside.
本节采用一个元素列表,每个元素都包含一个 profile-id。
<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.
可以根据检测到的构建环境的 state 自动触发配置文件。 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.
目前,此检测仅限于 JDK 版本的前缀匹配、系统属性的存在或系统属性的值。
This allows to create a different package content based on the target environment.这允许根据目标环境创建不同的 package 内容。
If I understood your problem correctly then you can use " Maven Assembly plugin " and " The Assembly Descriptor " in the following way: first of all, you can not use a profile if you want to build 2 jar files simultaneously.如果我正确理解您的问题,那么您可以通过以下方式使用“ Maven 程序集插件”和“程序集描述符”:首先,如果要同时构建 2 个 jar 文件,则不能使用配置文件。 So, my suggestion is to exclude your config files from your jar then use Maven Assembly plugin to create a different zip file with these folders.
所以,我的建议是从你的 jar 中排除你的配置文件,然后使用 Maven 程序集插件创建一个不同的 zip 文件与这些文件夹。
for example, in your case you should have 2 file descriptors like the following:例如,在您的情况下,您应该有 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>
and folder1.xml
contains:和
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>
for "folder2" you can do it in the same way.对于“folder2”,您可以以相同的方式进行操作。
also for exclude some config files from the jar you can use this plugin:还可以从 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>
Using maven-resources-plugin as follows:使用 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.