2

我有一个 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>

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

4

1 回答 1

2

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

于 2018-05-30T23:20:23.940 回答