代码之家  ›  专栏  ›  技术社区  ›  Shea

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

  •  0
  • Shea  · 技术社区  · 7 年前

    我有一个pom文件,它使用了一个我想在外部groovy脚本中设置的属性。这个groovy脚本需要一些现有的属性来确定设置新属性是什么,但是,当我设置属性时 bindPropertiesToSeparateVariables 要设置为false(在脚本执行中),所有必需的属性都将公开给groovy脚本,但我无法设置新属性。( project.properties.setProperty('myproperty', value) 抱怨“项目”不存在,以及 properties.setProperty('myproperty', value) 绑定属性与辅助属性 为true,并不是所有必需的属性都公开给groovy脚本( project.properties 没有所有属性),但我可以使用 project.properties.setproperty('myproperty',值) 设置成功。

    我有点困惑到底是什么 绑定属性与辅助属性 是的,因为对财产的描述基本上只是在重述以财产的名义已经存在的东西。我试过用 parent.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 回复  |  直到 7 年前
        1
  •  0
  •   Shea    7 年前

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