首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用使用gmavenplus运行的groovy脚本设置maven项目属性?

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

Stack Overflow用户
提问于 2018-05-29 19:48:53
回答 1查看 2.2K关注 0票数 2

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

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

下面是执行脚本的pom文件的一部分。最后一行是我需要访问我的新属性的地方。

代码语言:javascript
复制
<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>

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-30 23:20:23

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50591898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档