带有扩展功能的Maven 3配置文件 [英] Maven 3 profile with extensions

查看:164
本文介绍了带有扩展功能的Maven 3配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题已在此线程中得到解决,但是解释不清楚.

My question had been addressed in this thread, but the explanation is not clear.

我的一个pom.xml文件中具有以下构建定义:

I have this build definition in one of my pom.xml files:

<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不允许在配置文件下使用扩展名标签.

Notice that I'm using the maven-s3-wagon extension. Next, I would like to have 2 different profiles, each with it's own settings, plugins and extensions but maven does not allow the extensions tag under a profile.

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

When I try using a profile:

<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.

问题,那么使用扩展标签意味着我无法使用个人资料?如何通过个人资料使用或更改构建扩展?

Question So using the extension tag means I can't use profiles? How can I use or change build extensions via profile?

推荐答案

确实是官方 Maven POM reference 不清楚extensions作为Maven配置文件的一部分的可能用法,因为它指出您可以在其中包含build元素,但不能包含build部分的内容.

Indeed, the official Maven POM reference is not clear about the possible usage of extensions as part of a Maven profile, since it states you can have a build element within it, but not what of the build section.

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

However, the official Maven model effectively filters and provides what of the build section you can actually use within a profile section. And indeed extensions is not there.

但是,什么是Maven扩展?构建/生命周期增强,但也(本质上):向Maven构建的运行时类路径添加了一个库,该库参与了构建,但未与最终工件打包在一起.

However, what are Maven extensions? Build/Lifecycle enhancement, but also (and essentially): a library added to the runtime classpath of the Maven build, which participates to the build, but it is not packaged with the final artifact.

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

Hence, in such a scenario (if you need to have extensions in profile or have a profile to change/add an extension) you could use the following trick:

  • 具有一个无害的扩展名作为构建的默认扩展名(无害意味着任何可能属于构建类路径并且根本不影响它的库)
  • 具有定义此扩展程序的GAV坐标( G roupId, A rtifactId, V 版本)的属性
  • 具有一个配置文件,该配置文件使用所需的(有用的)扩展名覆盖了这些属性
  • Have an harmless extension as default extension of your build (where harmless means whatever library which could be part of your build classpath and essentially not affect it at all)
  • Have properties defining the GAV coordinates (GroupId, ArtifactId, Version) of this extension
  • Have a profile which overrides these properties with the desired (useful) extension

作为示例,给定以下样本POM:

As an example, given the following sample 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版本产生冲突)的版本,因此请确保使用相同的版本,并为此使用更加无害的库.)

The default build (without the customize-extension profile activated), would use the default defined properties and as such add junit as build extension: this is harmless (although it may create conflicts with another junit version of your build, so make sure you use the same version of use an even more harmless library for that).

您可以通过运行第一个构建阶段,仅用于检查本例中的信息并启用调试标志:

You can check Maven will pick it up by running a really first build phase, just to check information in our case, and enable the debug flag:

mvn initialize -X

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

And checking as part of the build log:

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

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

Now let's use our trick: let's add (change) a build extension via profile:

mvn initialize -X -Pcustomize-extension

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

And as part of our build log we would have:

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

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

Bingo. Maven picked up a different extension (in this case, a different version, the 4.12) and we succeeded on changing (or actually adding a meaningful) build extension via profile.

这篇关于带有扩展功能的Maven 3配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆