首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Maven Properties插件不拾取值

Maven Properties插件不拾取值
EN

Stack Overflow用户
提问于 2019-07-05 19:03:21
回答 1查看 915关注 0票数 0

我有一个带有父模块和2个子模块的Maven项目(一个子模块构建一个JAR,一个子模块与依赖于前者的子模块构建战争)。我已经让项目成功地使用WildFly插件来运行应用服务器。我现在正试图更新这个插件,以使用属性文件而不是内联字符串作为WildFly服务器的路径,并通过配置文件拥有不同的环境。下面是我到目前为止所拥有的(很多是测试代码)。

local.properties

代码语言:javascript
复制
local.wildfly.path=/Users/snowyCoderGirl/wildfly-8.2.1.Final

staging.properties

代码语言:javascript
复制
staging.wildfly.path=/Users/snowyCoderGirl/wildfly-8.2.1.Final

production.properties

代码语言:javascript
复制
production.wildfly.path=/Users/snowyCoderGirl/wildfly-8.2.1.Final

pom.xml

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

代码语言:javascript
复制
<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中,它允许我像下面的屏幕快照那样自动完成属性文件值,但是当它自动完成时,它会给出一个不能解析符号的错误。

更新

见评论部分。

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

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

代码语言:javascript
复制
<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时,它似乎得到了正确的值,如下所示。

代码语言:javascript
复制
[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时,将捕获正确的值(如下面所示)(最后一行)。

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

回答 1

Stack Overflow用户

发布于 2019-07-12 20:00:42

在运行mvn wildfly:run时,只执行wildfly-maven-pluginrun目标。它是通过以下配置执行的:

代码语言:javascript
复制
<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文件中的属性没有被读取,因为properties-maven-plugin 没有被执行(检查Maven日志,查看这个插件没有被调用)。

因此,由于wildfly.path有一个空值,所以wildfly-maven-plugin的行为就好像没有设置该属性一样,从而下载了Wildfly的最新版本。

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

代码语言:javascript
复制
mvn initialize wildfly:run

由于properties-maven-plugin被绑定到initialize阶段,所以wildfly.path属性将在initialize阶段期间设置。

这也解释了为什么mvn package wildfly:run工作,因为使用此命令,initialize阶段将作为package阶段之前的所有阶段运行。

请注意,您还可以运行:

代码语言:javascript
复制
mvn wildfly:run -Dwildfly.path=/path/to/wildfly-8.2.0.Final

在这里,命令中提供了wildfly.path,因此不需要从属性文件中读取。

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

https://stackoverflow.com/questions/56908109

复制
相关文章

相似问题

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