簡體   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