Maven动态插件配置



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

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

是否存在配置文件继承?例如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>

区别仅在于配置文件2没有<jvmArgs>

如何重用 99% 的 xml 脚本?

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

然后,对于 Mac 版本:

mvn ... -P all ...

对于 Windows/Unix 版本:

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

您还可以将反向逻辑或其组合与停用配置文件一起使用。

为了解决您的具体示例 - 我建议为 jvmargs 使用属性。例如,下面是一个简单的 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 的回答,如果你有一个更复杂的场景 - 比如你需要为构建指定不同的插件 - 你可以创建多个配置文件并激活多个配置文件。这样,您的常用插件和配置可以放在一个配置文件中,而特定于特定环境/场景的元素可以进入另一个配置文件。

希望这有帮助!

最新更新