如何使用gmavenplus运行的groovy脚本设置maven项目属性?

时间:2018-05-29 19:48:53

标签: maven groovy pom.xml gmavenplus

我有一个pom文件,它使用我想在外部groovy脚本中设置的属性。这个groovy脚本需要一些现有属性来确定设置新属性的内容,但是,当我将属性bindPropertiesToSeparateVariables设置为false(在脚本执行中)时,所有必需的属性都会暴露给groovy脚本,但是我无法设置我的新属性(project.properties.setProperty('myproperty', value)抱怨'项目'不存在,properties.setProperty('myproperty', value)不起作用)。当我将bindPropertiesToSeparateVariables设置为true时,并非所有必需的属性都暴露给groovy脚本(project.properties没有所有属性),但我可以使用project.properties.setProperty('myproperty', value)设置我的新属性,它设置成功。

我对bindPropertiesToSeparateVariables究竟是做什么感到有点困惑,因为该属性的描述基本上只是重述了属性名称中的内容。我尝试使用parent.properties,但这不起作用。我可以在插件的配置中手动定义属性(正好在执行脚本的位置上方),然后使用project.properties访问它们吗?如果这样可行,如果添加了我没有手动定义的新属性怎么办? session.properties有效吗?

这是我执行脚本的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>

    <parent>
        <groupId>com.example.group</groupId>
        <artifactId>example-parent-pom</artifactId>
        <version>1.0.0</version>
        <relativePath>../../..</relativePath>
    </parent>

    <groupId>com.example.group</groupId>
    <artifactId>example.artifact.id</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <someOtherProperty>someValue</someOtherProperty>
        <anotherProperty>anotherValue</anotherProperty>
    </properties>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <configuration>
                        <outputDirectory>${project.build.directory}/sql</outputDirectory>
                    </configuration>
                    <executions>
                        <!-- copy the basescript groovy files so they are accessible on the classpath during script execution -->
                        <execution>
                            <id>Copy basescripts</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${project.build.directory}/classes/</outputDirectory>
                                <resources>
                                    <resource>
                                        <directory>${basedir}/../../../config/build/scripts</directory>
                                        <includes>
                                            <include>**/*.groovy</include>
                                        </includes>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                        <execution>
                            <id>Copy some of the scripts we use</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>${project.build.directory}/classes/</outputDirectory>
                                <resources>
                                    <resource>
                                        <directory>${basedir}/../scripts</directory>
                                        <includes>
                                            <include>**/SomeScriptWeUse.groovy</include>
                                            <include>**/SomeOtherScriptWeUse.groovy</include>
                                        </includes>
                                    </resource>
                                </resources>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>resources</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <configuration>
                    <allowSystemExits>true</allowSystemExits>
                    <skip>someValue</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>set-myProperty</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <scripts>
                                <script>file:///${basedir}/../scripts/setMyProperty.groovy</script>
                            </scripts>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.example.another.group.id</groupId>
                <artifactId>homemade-plugin</artifactId>
                <configuration>
                    <skip>${someSkipProp}</skip>
                    <verbose>true</verbose>
                    <forceRemove>true</forceRemove>
                    <someVersionProp>1</someVersionProp>
                </configuration>
                <executions>
                    <execution>
                        <id>Just another execution</id>
                        <goals>
                            <goal>some-goal</goal>
                        </goals>
                        <phase>package</phase>
                        <configuration>
                            <someProperty>${myProperty}</someProperty>
                        </configuration>
                    </execution>

欢迎任何建议。如果需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

您可以将bindPropertiesToSeparateVariables设置为false,并将properties.setProperty('myproperty', value)替换为properties.project.properties.setProperty('myproperty', value)。然后,要在pom文件中进一步向下访问该属性,请将其引用为${myproperty}。不要在pom文件中的任何位置定义<myproperty>value</myproperty,否则这将不起作用。