Maven属性插件未获取值

时间:2019-07-05 19:03:21

标签: maven wildfly

我有一个Maven项目,其中有一个父级和2个子级模块(一个子级构建JAR,一个子级构建WAR,后者依赖于前者)。我已经获得了成功使用WildFly插件运行应用程序服务器的项目。我现在正在尝试更新该插件,以使用属性文件代替内联字符串作为WildFly服务器的路径,并通过配置文件使用不同的环境。以下是我到目前为止的内容(很多是测试代码)。

local.properties

local.wildfly.path=/Users/snowyCoderGirl/wildfly-8.2.1.Final

staging.properties

staging.wildfly.path=/Users/snowyCoderGirl/wildfly-8.2.1.Final

production.properties

production.wildfly.path=/Users/snowyCoderGirl/wildfly-8.2.1.Final

pom.xml

<properties>
    <parentDirectory>${project.basedir}</parentDirectory>
    <skip.maven.wildfly>true</skip.maven.wildfly>
</properties>

<profiles>
    <profile>
        <id>local</id>
        <properties>
            <wildfly.path>${local.wildfly.path}</wildfly.path>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>staging</id>
        <properties>
            <wildfly.path>${staging.wildfly.path}</wildfly.path>
        </properties>
    </profile>
    <profile>
        <id>production</id>
        <properties>
            <wildfly.path>${production.wildfly.path}</wildfly.path>
        </properties>
    </profile>
</profiles>

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>properties-maven-plugin</artifactId>
            <version>1.0.0</version>
            <executions>
                <execution>
                    <phase>initialize</phase>
                    <goals>
                        <goal>read-project-properties</goal>
                    </goals>
                    <configuration>
                        <files>
                            <file>${parentDirectory}/local.properties</file>
                            <file>${parentDirectory}/staging.properties</file>
                            <file>${parentDirectory}/production.properties</file>
                        </files>
                    </configuration>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.wildfly.plugins</groupId>
            <artifactId>wildfly-maven-plugin</artifactId>
            <version>2.0.1.Final</version>
            <configuration>
                <jboss-home>${wildfly.path}</jboss-home>
                <skip>${skip.maven.wildfly}</skip>
            </configuration>
        </plugin>
    </plugins>
</build>

childModule2 / pom.xml

<properties>
    <parentDirectory>${project.basedir}/..</parentDirectory>
    <skip.maven.wildfly>false</skip.maven.wildfly>
</properties>

使用上述方法,当我运行mvn wildfly:run时,它将下载最新的WildFly服务器并尝试使用该服务器而不是local.wildfly.path来运行。

如果我更改local配置文件,使其直接使用路径(而不是使用local.wildfly.path,则它将按预期工作。

我不确定我是否正确配置了属性插件,或者父/子模块关系是否以某种方式干扰了事情。我确实注意到奇怪的是,在IntelliJ中,它允许我自动完成属性文件的值,如下面的屏幕截图所示,但是当它自动完成时,会出现关于无法解析符号的错误。

IntelliJ allows autocomplete IntelliJ complains about not being about to resolve value

更新

请参阅评论部分。

我意识到mvn clean package wildfly:run正确地获取了正确的属性值。但是mvn clean wildfly:runmvn wildfly:run不会获取值。

我将以下内容添加到了我的父母pom.xml中。

<plugin>
    <groupId>com.soebes.maven.plugins</groupId>
    <artifactId>echo-maven-plugin</artifactId>
    <version>0.3.0</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>echo</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <echos>
            <echo>Discovered WildFly Path: ${wildfly.path}</echo>
        </echos>
    </configuration>
</plugin>

当我运行mvn wildfly:run时,看起来它正在获取正确的值,如下面的输出所示。

[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO] 
[INFO] parent                                                         [pom]
[INFO] child-jar                                                      [jar]
[INFO] child-war                                                      [war]
[INFO] 
[INFO] -----------------------< com.mycompany:parent >-----------------------
[INFO] Building parent 1.0-SNAPSHOT                                   [1/3]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO] 
[INFO] >>> wildfly-maven-plugin:2.0.1.Final:run (default-cli) > package @ parent >>>
[INFO] 
[INFO] --- properties-maven-plugin:1.0.0:read-project-properties (default) @ parent ---
[INFO] 
[INFO] --- echo-maven-plugin:0.3.0:echo (default) @ parent ---
[INFO] Discovered WildFly Path: /Users/snowyCoderGirl/wildfly-8.2.1.Final
[INFO] 
[INFO] <<< wildfly-maven-plugin:2.0.1.Final:run (default-cli) < package @ parent <<<
[INFO] 
[INFO] 
[INFO] --- wildfly-maven-plugin:2.0.1.Final:run (default-cli) @ parent ---
[INFO] 
[INFO] ---------------------< com.mycompany:child-jar >---------------------
[INFO] Building child-jar 1.0-SNAPSHOT                               [2/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] >>> wildfly-maven-plugin:2.0.1.Final:run (default-cli) > package @ child-jar >>>
[INFO] 
[INFO] --- properties-maven-plugin:1.0.0:read-project-properties (default) @ child-jar ---
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ child-jar ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ child-jar ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ child-jar ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/snowyCoderGirl/IdeaProjects/myproject/child-jar/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ child-jar ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ child-jar ---
[INFO] Surefire report directory: /Users/snowyCoderGirl/IdeaProjects/myproject/child-jar/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
... // Excluded test output here

Results :

Tests run: 40, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ child-jar ---
[INFO] 
[INFO] --- echo-maven-plugin:0.3.0:echo (default) @ child-jar ---
[INFO] Discovered WildFly Path: /Users/snowyCoderGirl/wildfly-8.2.1.Final
[INFO] 
[INFO] <<< wildfly-maven-plugin:2.0.1.Final:run (default-cli) < package @ child-jar <<<
[INFO] 
[INFO] 
[INFO] --- wildfly-maven-plugin:2.0.1.Final:run (default-cli) @ child-jar ---
[INFO] 
[INFO] ------------------------< com.mycompany:child-war >------------------------
[INFO] Building child-war 1.0-SNAPSHOT                                     [3/3]
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] >>> wildfly-maven-plugin:2.0.1.Final:run (default-cli) > package @ child-war >>>
[INFO] 
[INFO] --- properties-maven-plugin:1.0.0:read-project-properties (default) @ child-war ---
[INFO] 
[INFO] --- apt-maven-plugin:1.1.3:process (default) @ child-war ---
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ child-war ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 159 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ child-war ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 747 source files to /Users/snowyCoderGirl/IdeaProjects/myproject/child-war/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ child-war ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/snowyCoderGirl/IdeaProjects/myproject/child-war/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ child-war ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ child-war ---
[INFO] Surefire report directory: /Users/snowyCoderGirl/IdeaProjects/myproject/child-war/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
... // Excluded test output here

Results :

Tests run: 163, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-war-plugin:2.2:war (default-war) @ child-war ---
[INFO] Packaging webapp
[INFO] Assembling webapp [child-war] in [/Users/snowyCoderGirl/IdeaProjects/myproject/child-war/target/child-war-1.0-SNAPSHOT]
[INFO] Processing war project
[INFO] Copying webapp resources [/Users/snowyCoderGirl/IdeaProjects/myproject/child-war/src/main/webapp]
[INFO] Webapp assembled in [1475 msecs]
[INFO] Building war: /Users/snowyCoderGirl/IdeaProjects/myproject/child-war/target/child-war-1.0-SNAPSHOT.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO] 
[INFO] --- echo-maven-plugin:0.3.0:echo (default) @ child-war ---
[INFO] Discovered WildFly Path: /Users/snowyCoderGirl/wildfly-8.2.1.Final
[INFO] 
[INFO] <<< wildfly-maven-plugin:2.0.1.Final:run (default-cli) < package @ child-war <<<
[INFO] 
[INFO] 
[INFO] --- wildfly-maven-plugin:2.0.1.Final:run (default-cli) @ child-war ---
[INFO] JAVA_HOME : /Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre
[INFO] JBOSS_HOME: /Users/snowyCoderGirl/IdeaProjects/myproject/child-war/target/wildfly-17.0.0.Final

但是,如最后一行所示,它最终使用下载的WildFly 17结束。相反,当我运行mvn clean package wildfly:run时,则(如下所示)将获取正确的值(最后一行)。

[INFO] WEB-INF/web.xml already added, skipping
[INFO] 
[INFO] --- echo-maven-plugin:0.3.0:echo (default) @ child-war ---
[INFO] Discovered WildFly Path: /Users/snowyCoderGirl/wildfly-8.2.1.Final
[INFO] 
[INFO] <<< wildfly-maven-plugin:2.0.1.Final:run (default-cli) < package @ child-war <<<
[INFO] 
[INFO] 
[INFO] --- wildfly-maven-plugin:2.0.1.Final:run (default-cli) @ child-war ---
[INFO] JAVA_HOME : /Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre
[INFO] JBOSS_HOME: /Users/snowyCoderGirl/wildfly-8.2.1.Final

1 个答案:

答案 0 :(得分:0)

运行mvn wildfly:run时,仅执行run的{​​{1}}目标。它使用以下配置执行:

wildfly-maven-plugin

但是,插件希望定义<plugin> <groupId>org.wildfly.plugins</groupId> <artifactId>wildfly-maven-plugin</artifactId> <version>2.0.1.Final</version> <configuration> <jboss-home>${wildfly.path}</jboss-home> <skip>${skip.maven.wildfly}</skip> </configuration> </plugin> 属性。但是在这里,使用wildfly.path命令未定义mvn wildfly:run:尚未读取文件的属性,因为尚未执行wildfly.path (请检查Maven日志以查看尚未调用此插件)。

因此,由于properties-maven-plugin的值为空,wildfly.path的行为就像未设置此属性一样,因此下载了最新版本的Wildfly。

要确保设置了wildfly-maven-plugin属性,您应该通过运行以下命令来确保wildfly.path从文件中读取属性:

properties-maven-plugin

由于mvn initialize wildfly:run 绑定到properties-maven-plugin阶段,因此initialize属性将在wildfly.path阶段设置。

这也解释了initialize起作用的原因,因为使用此命令,mvn package wildfly:run阶段与initialize阶段之前的所有阶段一样运行。

请注意,您也可以运行:

package

此处,命令中提供了mvn wildfly:run -Dwildfly.path=/path/to/wildfly-8.2.0.Final ,因此无需从属性文件中读取。