繁体   English   中英

带有扩展的Maven 3配置文件

[英]Maven 3 profile with extensions

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

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

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

<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官方MOM参考尚未明确将extensions作为Maven概要文件的一部分的用途,因为它指出您可以在其中包含一个build元素,但不能包含build部分的内容。

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

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

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

  • 具有无害的扩展名作为构建的默认扩展名(无害意味着可以成为构建类路径的一部分而本质上完全不影响它的任何库)
  • (G roupId,A rtifactId,V版为)此扩展的定义GAV坐标属性
  • 具有使用所需(有用)扩展名覆盖这些属性的配置文件

例如,给定以下示例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版本产生冲突,因此请确保使用相同的版本使用更无害的库)。

您可以通过运行一个真正的第一个构建阶段来检查Maven是否将其选中,以检查我们的案例中的信息并启用debug标志:

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 ),我们成功地通过配置文件更改了(或实际上添加了一个有意义的)构建扩展。

只是一个疯狂的主意: 使用模块

定义这样的父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上定义所需的扩展。

我认为解决方案在这里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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM