Maven动态插件配置

时间:2018-08-06 19:28:08

标签: maven maven-profiles

我在POM中有这样的内容:

    <profile>
        <id>profile1</id>
        <build>
            <resources>
                ....
            </resources>

            <plugins>
                <plugin>
                    <configuration>
                        ....plugin1 configuration.....
                    </configuration>
                </plugin>
                <plugin>
                    <configuration>
                        ....plugin2 configuration.....
                        <configEntry1></configEntry1>
                        <configEntry2></configEntry1>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>

个人资料只有几个插件,这些插件具有配置和执行。 根据Jenkins的工作(我想在Win,Unix,Mac OS上构建桌面Java应用程序),插件配置可能会有小的变化。例如,如果我为Mac运行该作业,则第二个插件配置不应具有configEntry2

我必须为每个作业创建配置文件,并且必须复制配置文件XML配置的99%粘贴。有没有办法重复使用类似的零件?

是否有Profile继承?例如profile2profile3可以继承profile1的所有插件,其执行和配置,但是我将覆盖一个插件的配置。

还是可以根据属性设置动态配置?例如:<configEntry2 ifTrue="${property.name}"></configEntry1>。不要

有这样的东西吗?

谢谢您的回答。

编辑-添加一个具体示例:

配置文件1:

<profile>
    <id>profile1</id>
    <plugin>
        <groupId>com.zenjava</groupId>
        <artifactId>javafx-maven-plugin</artifactId>
        <version>8.8.3</version>
        <configuration>
            <identifier>fxApplication</identifier>
            <appName>AppName</appName>
            <mainClass>package.Main</mainClass>
            <nativeReleaseVersion>${jfx.version}</nativeReleaseVersion>
            <jfxMainAppJarName>jarName.jar</jfxMainAppJarName>
            <deployDir>${basedir}/deploy</deployDir>            
            <updateExistingJar>true</updateExistingJar>
            <manifestAttributes>
                <JavaFX-Feature-Proxy>None</JavaFX-Feature-Proxy>
                <Implementation-Vendor>vendor</Implementation-Vendor>
                <Implementation-Title>Title</Implementation-Title>              
                <Main-Class>package.Main</Main-Class>
                <JavaFX-Version>2.0</JavaFX-Version>
                <JavaFX-Application-Class>package.Main</JavaFX-Application-Class>
                <Created-By>Company</Created-By>
            </manifestAttributes>
            <jvmArgs>
                <jvmArg>-XX:-UseParallelGC -XX:+HeapDumpOnOutOfMemoryError -Xmx8G -XX:+UseG1GC -XX:InitiatingHeapOccupancyPercent=10 -XX:MaxHeapFreeRatio=40</jvmArg>-->
            </jvmArgs>
            <additionalAppResources>${basedir}/target/jfx/externalResources</additionalAppResources>
        </configuration>

        <executions>
            <execution>             
                <id>create-jfxjar</id>
                <phase>package</phase>
                <goals>
                    <goal>build-jar</goal>
                </goals>
            </execution>
            <execution>
                <id>create-native</id>
                <phase>package</phase>
                <goals>
                    <goal>build-native</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</profile>

和个人资料2:

<profile>
    <id>profile2</id>
    <plugin>
        <groupId>com.zenjava</groupId>
        <artifactId>javafx-maven-plugin</artifactId>
        <version>8.8.3</version>
        <configuration>
            <identifier>fxApplication</identifier>
            <appName>AppName</appName>
            <mainClass>package.Main</mainClass>
            <nativeReleaseVersion>${jfx.version}</nativeReleaseVersion>
            <jfxMainAppJarName>jarName.jar</jfxMainAppJarName>
            <deployDir>${basedir}/deploy</deployDir>            
            <updateExistingJar>true</updateExistingJar>
            <manifestAttributes>
                <JavaFX-Feature-Proxy>None</JavaFX-Feature-Proxy>
                <Implementation-Vendor>vendor</Implementation-Vendor>
                <Implementation-Title>Title</Implementation-Title>              
                <Main-Class>package.Main</Main-Class>
                <JavaFX-Version>2.0</JavaFX-Version>
                <JavaFX-Application-Class>package.Main</JavaFX-Application-Class>
                <Created-By>Company</Created-By>
            </manifestAttributes>           
            <additionalAppResources>${basedir}/target/jfx/externalResources</additionalAppResources>
        </configuration>

        <executions>
            <execution>             
                <id>create-jfxjar</id>
                <phase>package</phase>
                <goals>
                    <goal>build-jar</goal>
                </goals>
            </execution>
            <execution>
                <id>create-native</id>
                <phase>package</phase>
                <goals>
                    <goal>build-native</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
</profile>

区别仅在于profile2没有<jvmArgs>

如何重用99%的xml脚本?

2 个答案:

答案 0 :(得分:0)

您当然可以声明,但也可以同时激活多个配置文件。用<id>all</id>创建一个包含所有共同的声明以及扩展该声明的其他声明,例如WinUnix,其中包含<configEntry2>

然后,对于Mac版本:

mvn ... -P all ...

以及对于Windows / Unix构建:

mvn ... -P all,WinUnix ...

您还可以在Deactivating a profile中使用反向逻辑或其组合。

答案 1 :(得分:0)

为解决您的具体示例-我建议对jvmargs使用property。例如,下面是一个简单的POM,它在构建过程中会打印一条消息:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>maven-profiles</groupId>
    <artifactId>maven-profile-artifact</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <specialMessage>This is the default value</specialMessage>
    </properties>
    <profiles>
        <profile>
            <id>profile1</id>
            <properties>
                <specialMessage>This comes from profile1</specialMessage>
            </properties>
        </profile>
    </profiles>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <echo>${specialMessage}</echo>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

当没有配置文件处于活动状态时,请注意第一个properties标签如何指定默认消息。在profile1的后面,为该属性提供了一个特定值,以表明profile1是活动的。这样,构建标记及其所有子代便是公用/共享的,但是消息会根据profile1是否处于活动状态而更改。

我也同意Gerold Broser's answer的观点,如果您有一个更复杂的场景(例如,您需要为构建指定不同的插件),则可以创建多个配置文件并激活多个配置文件。这样,您的常用插件和配置可以进入一个配置文件,而特定于特定环境/场景的元素可以进入另一个配置文件。

希望这会有所帮助!

相关问题