【问题标题】:How to maven release multiple profiles project when redeploys are disabled禁用重新部署时如何发布多个配置文件项目
【发布时间】:2013-04-09 02:40:21
【问题描述】:

问题: 当在 Nexus 上禁用重新部署时,我应该如何发布具有 2 个专有配置文件的 maven 项目?

示例(已编辑): 我有一个 Maven 项目 MyArtifact,在 pom.xml(P1 和 P2)中有 2 个配置文件,它生成 2 个不同的耳朵。每个配置文件都将maven-ear-plugin 配置为包含不同的模块并自动生成application.xml。生成的工件是MyArtifact-1.0-P1.ear(2 个战争模块)和MyArtifact-1.0-P2.ear(3 个战争模块)。

问题 1(重新部署到 nexus)

  1. 当我执行“mvn deploy -P P1”时,一切正常(war 和 pom 已部署到 Nexus)
  2. 当我执行“mvn deploy -P P2”时出错! Nexus 抱怨重新部署 pom.xml。

问题 2(maven-release-plugin)

当使用maven-release-plugin 为多个配置文件发布时,maven 会做很多事情(签出和标记 CSM、更新 pom 版本、转向标记、提交到 CSM 等...)。至少对于每个配置文件执行都必须重新发布/重新标记是不高效也不实际的。

【问题讨论】:

    标签: maven maven-2 maven-3 nexus


    【解决方案1】:

    简单的答案。这是不可能的。问题在于在这种关系中使用了邪恶的配置文件。

    此类事情的解决方案是创建一个 Maven 构建,它能够生成两个作为此构建结果的 war 文件。

    我假设您对不同的环境有不同的属性,如下所示:

    .
    |-- pom.xml
    `-- src
        |-- main
        |   |-- java
        |   |-- resources
        |   |-- environment
        |   |   |-- test
        |   |   |   `-- database.properties
        |   |   |-- qa
        |   |   |   `-- database.properties
        |   |   `-- production
        |   |       `-- database.properties
        |   `-- webapp
    

    这意味着您需要在您的情况下创建两个仅在属性文件内容上有所不同的战争文件。在上面有三个环境。

    您需要的是每个环境的程序集描述符,例如:

    <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    
      <id>test</id>
      <formats>
        <format>war</format>
      </formats>
      <includeBaseDirectory>false</includeBaseDirectory>
      <dependencySets>
        <dependencySet>
          <unpack>true</unpack>
          <useProjectArtifact>true</useProjectArtifact>
        </dependencySet>
      </dependencySets>
      <fileSets>
        <fileSet>
          <outputDirectory>WEB-INF</outputDirectory>
          <directory>${basedir}/src/main/environment/test/</directory>
          <includes>
            <include>**</include>
          </includes>
        </fileSet>
      </fileSets>
    </assembly>
    

    在这种情况下,maven-assembly-plugin 的适当执行如下:

     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <id>test</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptors>
                <descriptor>${project.basedir}/src/main/assembly/test.xml</descriptor>
              </descriptors>
            </configuration>
          </execution>
          <execution>
            <id>qa</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptors>
                <descriptor>${project.basedir}/src/main/assembly/qa.xml</descriptor>
              </descriptors>
            </configuration>
          </execution>
          <execution>
            <id>production</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptors>
                <descriptor>${project.basedir}/src/main/assembly/production.xml</descriptor>
              </descriptors>
            </configuration>
          </execution>
        </executions>
      </plugin>
    

    这将通过以下方式运行一次 maven:

    mvn package
    

    这会产生三个不同的战争文件,其中包含不同的属性文件或具有不同内容的文件。 这是没有配置文件的解决方案,它可以完美运行,并将部署三个不同的 war 文件,命名如下:

    1. artifactId-version-test.war
    2. artifactId-version-qa.war
    3. artifactId-version-production.war

    这就是您解决问题所需要的结果。

    【讨论】:

    • 建议的解决方案可能是某种“简单”项目(如您的示例中的项目)的“解决方法”。但是,我认为它并不能解决真正的问题。我不认为解决方案是通过完成 war-plugin 或 ear-plugin 所做的所有工作来实现的。此外,如果我的配置文件属性正在调节生成的二进制文件,那么建议的解决方案可能不起作用。例如,¿您将如何有条件地检测代码或使用程序集描述符自动生成 application.xml?我开始认为这可能是一个 maven/nexus 集成问题。
    • 问题是你定义为真正的问题。除此之外,运行 ear/war 插件的时间已经完成。所以它不能代替它。 application.xml 的生成已经是那个时候了。您正在撰写有关配置文件属性的文章。根据原始帖子,与环境相关的配置文件属性是错误的方式。您希望在什么情况下检测代码?基本点是生成的战争/耳朵不应该依赖于环境。
    • 我有一个包含 2 个配置文件的项目:每个配置文件以不同的方式配置 ear 插件:配置文件“A”有 2 个战争模块,配置文件“B”有 3 个战争模块。在这两个配置文件中,类都使用 Jibx(具有不同的描述符)进行检测。忘记仪器的事情,¿如果不执行 2 次 ear-plugin,如何生成 2 个不同的 application.xml?
    【解决方案2】:

    是的!

    为了使用配置文件,我正在使用 maven &lt;executions&gt; 功能:为我拥有的每个配置文件执行 1 次。每次执行都必须在不同的工作文件夹中生成资源。每个工件都有不同的分类器。部署 maven 时会找到 3 个工件(pom、ear、ear)并将它们部署到 Nexus 上。

    下面是例子。如果您有任何问题,请告诉我:

    <plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-ear-plugin</artifactId>
            <executions>
                <execution>
                    <!-- Disable default execution -->
                    <id>default-ear</id>
                    <phase>none</phase>
                </execution>
                <execution>
                    <!-- Disable default execution -->
                    <id>default-generate-application-xml</id>
                    <phase>none</phase>
                </execution>
                <execution>
                    <!-- execution for profile P1 -->
                    <id>P1</id>
                    <goals>
                        <goal>ear</goal>
                        <goal>generate-application-xml</goal>
                    </goals>
                    <configuration>
                        <!-- Different working directory for each profile (very important) -->
                        <workDirectory>target/P1</workDirectory>
                        <defaultLibBundleDir>APP-INF/lib</defaultLibBundleDir>
                        <generateApplicationXml>true</generateApplicationXml>
                        <displayName>MyArtifactLibraryP1</displayName>
                        <!-- Different classifier for each profile (very important) -->
                        <classifier>P1</classifier>
                        <modules>
                            <jarModule>
                                <groupId>com.example</groupId>
                                <artifactId>MyWar1</artifactId>
                                <includeInApplicationXml>true</includeInApplicationXml>
                            </jarModule>
                            <jarModule>
                                <groupId>com.example</groupId>
                                <artifactId>MyWar2</artifactId>
                                <includeInApplicationXml>true</includeInApplicationXml>
                            </jarModule>
                        </modules>
                    </configuration>
                </execution>
                <execution>
                    <id>P2</id>
                    <goals>
                        <goal>ear</goal>
                        <goal>generate-application-xml</goal>
                    </goals>
                    <configuration>
                        <workDirectory>target/P2</workDirectory>
                        <defaultLibBundleDir>APP-INF/lib</defaultLibBundleDir>
                        <generateApplicationXml>true</generateApplicationXml>
                        <displayName>MyArtifactLibraryP2</displayName>
                        <classifier>P2</classifier>
                        <modules>
                            <jarModule>
                                <groupId>com.example</groupId>
                                <artifactId>MyWar1</artifactId>
                                <includeInApplicationXml>true</includeInApplicationXml>
                            </jarModule>
                            <jarModule>
                                <groupId>com.example</groupId>
                                <artifactId>MyWar2</artifactId>
                                <includeInApplicationXml>true</includeInApplicationXml>
                            </jarModule>
                            <jarModule>
                                <groupId>com.example</groupId>
                                <artifactId>MyWar3</artifactId>
                                <includeInApplicationXml>true</includeInApplicationXml>
                            </jarModule>
                        </modules>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    

    【讨论】:

      猜你喜欢
      • 2014-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-24
      • 1970-01-01
      相关资源
      最近更新 更多