使用 Maven 创建多个配置文件 jar
Create multiple profile jars using Maven
我有一个带有单个 POM 的 Maven 项目。我有多个配置文件和两个配置文件特定属性文件。
我想创建多个 jar 文件,每个配置文件 ID 一个。对于这个可加载的 jar,我需要添加特定于配置文件的 属性 文件。
罐子不应该有所有的依赖关系,只有所有 class 文件,这样每个罐子都可以作为其他 Maven 项目中的依赖关系。
问题:
- 有没有人以前做过这个,这可以在 Maven 中完成,还是我需要使用 antrun 构建器?
- 如果可以的话,最好的 Maven 插件组合是什么?
- 我正在生成其名称具有配置文件 ID 扩展名的属性文件,这些名称在添加到 jar 之前必须更改为默认名称。
- 这似乎很容易做一个或另一个....只是 jar 或只是属性文件...但是用特定的属性文件生成特定的 jar 是最痛苦的。
任何示例 POM xml 将不胜感激。
好吧,一种方法是通过 Maven 程序集。
结构:
multi-profile
|
+-- pom.xml
+-- profile-1.properties
+-- profile-2.properties
+-- src
|
+-- assembly
| |
| +-- profile-1.xml
| +-- profile-2.xml
|
+-- main
|
+-- java
|
+ ...
POM:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>multi-profile</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>multi-profile</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>create-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
<profiles>
<profile>
<id>profile-1</id>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/assembly/profile-1.xml</descriptor>
</descriptors>
<appendAssemblyId>true</appendAssemblyId>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>profile-2</id>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/assembly/profile-2.xml</descriptor>
</descriptors>
<appendAssemblyId>true</appendAssemblyId>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>profile-3</id>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/assembly/profile-1.xml</descriptor>
<descriptor>src/assembly/profile-2.xml</descriptor>
</descriptors>
<appendAssemblyId>true</appendAssemblyId>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
一个组件(profile-1.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>profile-1</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<files>
<file>
<source>${project.basedir}/profile-1.properties</source>
<outputDirectory>/</outputDirectory>
<destName>config.properties</destName>
</file>
</files>
<dependencySets>
<dependencySet>
<!-- This will exclude any transitive dependencies from being included in your assembly -->
<includes>
<include>org.example:multi-profile</include>
</includes>
<useTransitiveDependencies>false</useTransitiveDependencies>
<unpack>true</unpack>
</dependencySet>
</dependencySets>
</assembly>
如果您 运行 mvn clean install
则不会包含任何属性文件。如果您 运行 mvn -P profile-3 clean install
将在一个构建中创建所有配置文件(在单独的工件中)。程序集将根据需要重命名属性文件(在本例中为 config.properties
)。
如果其他一些项目需要这些 "profile builds" 之一作为依赖项,只需像这样引用它:
<dependency>
<groupId>org.example</groupId>
<artifactId>multi-profile</artifactId>
<version>1.0-SNAPSHOT</version>
<classifier>profile-1</classifier>
</dependency>
希望对你有用。
我有一个带有单个 POM 的 Maven 项目。我有多个配置文件和两个配置文件特定属性文件。
我想创建多个 jar 文件,每个配置文件 ID 一个。对于这个可加载的 jar,我需要添加特定于配置文件的 属性 文件。 罐子不应该有所有的依赖关系,只有所有 class 文件,这样每个罐子都可以作为其他 Maven 项目中的依赖关系。
问题:
- 有没有人以前做过这个,这可以在 Maven 中完成,还是我需要使用 antrun 构建器?
- 如果可以的话,最好的 Maven 插件组合是什么?
- 我正在生成其名称具有配置文件 ID 扩展名的属性文件,这些名称在添加到 jar 之前必须更改为默认名称。
- 这似乎很容易做一个或另一个....只是 jar 或只是属性文件...但是用特定的属性文件生成特定的 jar 是最痛苦的。
任何示例 POM xml 将不胜感激。
好吧,一种方法是通过 Maven 程序集。
结构:
multi-profile
|
+-- pom.xml
+-- profile-1.properties
+-- profile-2.properties
+-- src
|
+-- assembly
| |
| +-- profile-1.xml
| +-- profile-2.xml
|
+-- main
|
+-- java
|
+ ...
POM:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>multi-profile</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>multi-profile</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>create-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
<profiles>
<profile>
<id>profile-1</id>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/assembly/profile-1.xml</descriptor>
</descriptors>
<appendAssemblyId>true</appendAssemblyId>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>profile-2</id>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/assembly/profile-2.xml</descriptor>
</descriptors>
<appendAssemblyId>true</appendAssemblyId>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>profile-3</id>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/assembly/profile-1.xml</descriptor>
<descriptor>src/assembly/profile-2.xml</descriptor>
</descriptors>
<appendAssemblyId>true</appendAssemblyId>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
一个组件(profile-1.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>profile-1</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<files>
<file>
<source>${project.basedir}/profile-1.properties</source>
<outputDirectory>/</outputDirectory>
<destName>config.properties</destName>
</file>
</files>
<dependencySets>
<dependencySet>
<!-- This will exclude any transitive dependencies from being included in your assembly -->
<includes>
<include>org.example:multi-profile</include>
</includes>
<useTransitiveDependencies>false</useTransitiveDependencies>
<unpack>true</unpack>
</dependencySet>
</dependencySets>
</assembly>
如果您 运行 mvn clean install
则不会包含任何属性文件。如果您 运行 mvn -P profile-3 clean install
将在一个构建中创建所有配置文件(在单独的工件中)。程序集将根据需要重命名属性文件(在本例中为 config.properties
)。
如果其他一些项目需要这些 "profile builds" 之一作为依赖项,只需像这样引用它:
<dependency>
<groupId>org.example</groupId>
<artifactId>multi-profile</artifactId>
<version>1.0-SNAPSHOT</version>
<classifier>profile-1</classifier>
</dependency>
希望对你有用。