[英]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構建的運行時類路徑的庫,該庫參與構建,但未與最終工件打包在一起。
因此,在這種情況下(如果您需要在配置文件中具有擴展名或具有用於更改/添加擴展名的配置文件),則可以使用以下技巧:
例如,給定以下示例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>
在pom1
和pom2
上定義所需的擴展。
我認為解決方案在這里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.