【问题标题】:Create multiple profile jars using Maven使用 Maven 创建多个配置文件 jar
【发布时间】:2015-10-22 00:09:22
【问题描述】:

我有一个带有单个 POM 的 Maven 项目。我有多个配置文件和两个配置文件特定的属性文件。

我想创建多个 jar 文件,每个配置文件 id 一个。对于这个可加载的 jar,我需要添加配置文件特定的属性文件。 jar 不应该包含所有依赖项,只有所有类文件,以便每个 jar 都可以作为其他 Maven 项目中的依赖项。

问题:

  1. 以前有没有人这样做过,可以在 Maven 中完成,还是我需要使用 antrun builder?
  2. 如果可以的话,最好使用 Maven 插件组合是什么?
  3. 我正在生成名称具有配置文件 id 扩展名的属性文件,这些名称在添加到 jar 之前必须更改为默认名称。
  4. 似乎很容易做一个或另一个......只是 jar 或只是属性文件......但是用特定的属性文件生成特定的 jar 是最大的痛苦。

任何示例 POM xml 将不胜感激。

【问题讨论】:

  • 您可以通过多个插件执行来做到这一点,如this answer 中所述。
  • 你需要有不同的依赖关系,你需要从中分离出不同的模块。而且个人资料是错误的方式。

标签: maven jar pom.xml


【解决方案1】:

嗯,一种方法是通过 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>

希望它对你有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    • 2012-01-07
    • 2016-11-03
    • 2011-05-14
    • 1970-01-01
    • 2011-06-23
    • 1970-01-01
    相关资源
    最近更新 更多