簡體   English   中英

如何使用通過gmavenplus運行的Groovy腳本設置Maven項目屬性?

[英]How do I set a maven project property with a groovy script run with gmavenplus?

我有一個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確切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>

歡迎任何建議。 請讓我知道是否需要更多信息。

您可以將bindPropertiesToSeparateVariables設置為false,然后將properties.setProperty('myproperty', value)替換為properties.project.properties.setProperty('myproperty', value) 然后,要在pom文件中進一步訪問該屬性,請將其引用為${myproperty} 不要在pom文件中的任何位置定義<myproperty>value</myproperty ,否則將不起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM