Maven 3配置文件,带扩展名

for*_*has 10 build-process maven-3 maven maven-profiles maven-extension

我的问题已在这个帖子中得到解决,但解释并不清楚.

我在我的一个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>
Run Code Online (Sandbox Code Playgroud)

请注意,我正在使用maven-s3-wagon扩展.接下来,我想有两个不同的配置文件,每个配置文件都有自己的设置,插件和扩展名,但maven不允许在配置文件下使用扩展标记.

当我尝试使用配置文件时:

<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>
Run Code Online (Sandbox Code Playgroud)

我的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.
Run Code Online (Sandbox Code Playgroud)

问题所以使用扩展标签意味着我不能使用配置文件?如何通过个人资料使用或更改构建扩展?

A_D*_*teo 7

实际上,官方Maven POM参考文献并不清楚extensions作为Maven配置文件的一部分可能的用法,因为它声明你可以build在其中包含一个元素,但不是该build部分的内容.

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

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

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

  • 将无害扩展作为构建的默认扩展(其中无害意味着可能属于构建类路径的任何库,并且基本上不会影响它)
  • 具有定义此扩展的GAV坐标(G roupId,A rtifactId,V ersion )的属性
  • 使用所需(有用)扩展名覆盖这些属性的配置文件

作为示例,给出以下示例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>
Run Code Online (Sandbox Code Playgroud)

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

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

mvn initialize -X
Run Code Online (Sandbox Code Playgroud)

并检查构建日志的一部分:

[DEBUG] Populating class realm extension>junit:junit:4.11   
[DEBUG]   Included: junit:junit:jar:4.11   
Run Code Online (Sandbox Code Playgroud)

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

mvn initialize -X -Pcustomize-extension
Run Code Online (Sandbox Code Playgroud)

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

[DEBUG] Populating class realm extension>junit:junit:4.12   
[DEBUG]   Included: junit:junit:jar:4.12   
Run Code Online (Sandbox Code Playgroud)

答对了.Maven选择了一个不同的扩展(在这种情况下,是一个不同的版本4.12),我们成功地通过配置文件更改(或实际添加一个有意义的)构建扩展.