【问题标题】:Maven 3 profile with extensions带有扩展的 Maven 3 配置文件
【发布时间】:2014-12-26 22:21:54
【问题描述】:

this thread 已经解决了我的问题,但解释不清楚。

我的 pom.xml 文件之一中有此构建定义:

<build>
    <finalName>${my.project}</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.0</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
    <extensions>
        <extension>
            <groupId>org.kuali.maven.wagons</groupId>
            <artifactId>maven-s3-wagon</artifactId>
            <version>1.1.19</version>
        </extension>
    </extensions>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/settings.properties</include>
            </includes>
        </resource>
    </resources>
</build>

请注意,我正在使用 maven-s3-wagon 扩展。 接下来,我想要 2 个不同的配置文件,每个配置文件都有自己的设置、插件和扩展,但 maven 不允许在配置文件下使用 extensions 标签。

当我尝试使用个人资料时:

<profiles>
    <profile>
        <id>local-build</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <finalName>${my.project}</finalName>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.0</version>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                    </configuration>
                </plugin>
            </plugins>
            <extensions>
                <extension>
                    <groupId>org.kuali.maven.wagons</groupId>
                    <artifactId>maven-s3-wagon</artifactId>
                    <version>1.1.19</version>
                </extension>
            </extensions>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                    <includes>
                        <include>**/settings.properties</include>
                    </includes>
                </resource>
            </resources>
        </build>
    </profile>
</profiles>

我的 pom 中有一个错误:

cvc-complex-type.2.4.a: Invalid content was found starting with element 'extensions'. One of '{"http://maven.apache.org/POM/4.0.0":defaultGoal, "http://maven.apache.org/POM/
 4.0.0":resources, "http://maven.apache.org/POM/4.0.0":testResources, "http://maven.apache.org/POM/4.0.0":directory, "http://maven.apache.org/POM/4.0.0":filters, "http://
 maven.apache.org/POM/4.0.0":pluginManagement}' is expected.

问题那么使用扩展标签意味着我不能使用配置文件?如何通过配置文件使用或更改构建扩展?

【问题讨论】:

    标签: maven maven-3 build-process maven-profiles maven-extension


    【解决方案1】:

    只是一个疯狂的想法:使用模块

    像这样定义父 pom

    <groupId>org.example</groupId>
    <artifactId>my-parent</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>
    <profiles>
        <profile>
            <id>use-pom1</id>
            <modules>
                <module>pom1</module>
            </modules>
        </profile>
        <profile>
            <id>use-pom2</id>
            <modules>
                <module>pom2</module>
            </modules>
        </profile>
    </profiles>
    

    pom1pom2 上定义所需的扩展。

    【讨论】:

    • 确实是一个有趣的想法,但它可能会使事情复杂化。由于这是一个危险的生产系统,我试图找到一个更简单\稳定\官方\测试的解决方案。谢谢你
    • 好吧,你可以尝试使用maven-assembly-plugin,使用descriptor.xml。但我怀疑它会像你的扩展那样做。
    【解决方案2】:

    确实,官方Maven POM reference 并不清楚extensions 作为Maven 配置文件的一部分的可能用法,因为它声明您可以在其中包含build 元素,但不是build 的什么部分。

    但是,官方的Maven model 有效地过滤并提供了您可以在profile 部分中实际使用的build 部分。确实extensions不存在

    但是,什么是 Maven 扩展?构建/生命周期增强,但也(本质上):添加到 Maven 构建的运行时类路径的库,它参与构建,但不与最终工件打包。

    因此,在这种情况下(如果您需要在配置文件中有扩展名或有配置文件来更改/添加扩展名),您可以使用以下技巧:

    • 拥有一个无害的扩展作为构建的默认扩展(其中无害的意思是任何可能成为构建类路径的一部分并且根本不会影响它的库)
    • 具有定义此扩展程序的 GAV 坐标(GroupId、ArtifactId、Version)的属性
    • 拥有一个配置文件,用所需的(有用的)扩展名覆盖这些属性

    例如,给定以下示例 POM:

    <project>
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.sample</groupId>
        <artifactId>project</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <properties>
            <extension.groupId>junit</extension.groupId>
            <extension.artifactId>junit</extension.artifactId>
            <extension.version>4.11</extension.version>
        </properties>
    
        <build>
            <extensions>
                <extension>
                    <groupId>${extension.groupId}</groupId>
                    <artifactId>${extension.artifactId}</artifactId>
                    <version>${extension.version}</version>
                </extension>
            </extensions>
        </build>
    
        <profiles>
            <profile>
                <id>customize-extension</id>
                <properties>
                    <extension.groupId>junit</extension.groupId>
                    <extension.artifactId>junit</extension.artifactId>
                    <extension.version>4.12</extension.version>
                </properties>
            </profile>
        </profiles>
    
    </project>
    

    默认构建(未激活customize-extension 配置文件)将使用默认定义的properties 并因此添加junit 作为构建扩展:这是无害的(尽管它可能会与另一个junit 版本产生冲突您的构建版本,因此请确保您使用相同的版本,并为此使用更无害的库)。

    您可以通过运行真正的第一个 build phase 来检查 Maven 是否会接收它,只是为了检查我们案例中的信息,并启用调试标志:

    mvn initialize -X
    

    并作为构建日志的一部分进行检查:

    [DEBUG] Populating class realm extension>junit:junit:4.11   
    [DEBUG]   Included: junit:junit:jar:4.11   
    

    现在让我们使用我们的技巧:让我们通过配置文件添加(更改)构建扩展:

    mvn initialize -X -Pcustomize-extension
    

    作为我们构建日志的一部分,我们将拥有:

    [DEBUG] Populating class realm extension>junit:junit:4.12   
    [DEBUG]   Included: junit:junit:jar:4.12   
    

    宾果游戏。 Maven 选择了一个不同的扩展(在本例中是一个不同的版本,4.12),我们成功地通过配置文件更改(或实际上添加了一个有意义的)构建扩展。

    【讨论】:

      【解决方案3】:

      我认为解决方案在这里http://maven.apache.org/guides/mini/guide-using-extensions.html

      定义一个构建部分,其中定义了扩展,然后在配置文件中将属性设置为 true(如下所示的第二个配置文件)

      <build>
          <extensions>
              <extension>
                  <groupId>org.apache.maven.wagon</groupId>
                  <artifactId>wagon-ssh</artifactId>
                  <version>2.9</version>
              </extension>
          </extensions>
      </build>
      
      <profiles>
          <profile>
              <id>create-default</id>
              <activation>
                  <activeByDefault>true</activeByDefault>
                  <property>
                      <name>build</name>
                      <value>full</value>
                  </property>
              </activation>
              <build>
                  <plugins>
                      <plugin>
                          <groupId>org.springframework.boot</groupId>
                          <artifactId>spring-boot-maven-plugin</artifactId>
                          <executions>
                              <execution>
                                  <goals>
                                      <goal>repackage</goal>
                                  </goals>
                              </execution>
                          </executions>
                      </plugin>
                  </plugins>
              </build>
          </profile>
      
          <profile>
              <id>create-core</id>
              <activation>
                  <property>
                      <name>build</name>
                      <value>full</value>
                  </property>
              </activation>
              <build>
                  <plugins>
                      <plugin>
                          <groupId>org.apache.maven.plugins</groupId>
                          <artifactId>maven-jar-plugin</artifactId>
                          <extensions>true</extensions>
                          <version>2.6</version>
                          <configuration>
                              <finalName>import-station-core-${project.version}</finalName>
                          </configuration>
                          <executions>
                              <execution>
                                  <id>make-jar</id>
                                  <phase>package</phase>
                                  <goals>
                                      <goal>jar</goal>
                                  </goals>
                              </execution>
                          </executions>
                      </plugin>
                  </plugins>
              </build>
          </profile>
      </profiles>
      

      【讨论】:

      • 这里你所说的“然后在配置文件中设置属性为真(如下面显示的第二个配置文件)”是什么意思,你可以在第二个配置文件中指定你正在谈论的标签名称吗?这会很有帮助。或者,如果我只是在配置文件之外保留一个构建并且不在配置文件中添加任何特定的内容(不设置任何属性),它会起作用吗?谢谢
      猜你喜欢
      • 1970-01-01
      • 2012-01-29
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 2017-02-21
      • 2014-11-08
      • 1970-01-01
      相关资源
      最近更新 更多