我有一个带有单个POM的Maven项目。我有多个配置文件和两个配置文件特定的属性文件。
我想创建多个jar文件,每个配置文件ID一个。对于这个可加载的jar,我需要添加配置文件特定的属性文件。 jar不应该包含所有的依赖项,只有所有类文件,这样每个jar都可以作为其他Maven项目中的依赖项。
问题:
非常感谢任何POM xml示例。
答案 0 :(得分:2)
嗯,一种方法是通过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
)。
如果某个其他项目需要其中一个“配置文件构建”作为依赖项,只需像这样引用它:
<dependency>
<groupId>org.example</groupId>
<artifactId>multi-profile</artifactId>
<version>1.0-SNAPSHOT</version>
<classifier>profile-1</classifier>
</dependency>
希望它适合你。