java - 如何使用properties-maven-plugin?

标签 java maven

我想使用 properties-maven-plugin。我阅读了用法 http://mojohaus.org/properties-maven-plugin/usage.html ,但它对我不起作用。

我创建了一个非常简单的项目来测试它。这是我的 pom.xml:

<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>
  <groupId>test</groupId>
  <artifactId>MavenTest</artifactId>
  <version>1.0.0</version>
  <name>MavenTest</name>

  <properties>
    <prop1>2.2</prop1>
  </properties>

  <dependencies>
    <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j.version}</version>
        </dependency>
  </dependencies>

  <build>
    <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <version>1.0-alpha-2</version>
                <executions>
                    <execution>
                        <phase>initialize</phase>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>
                        <configuration>
                            <files>
                                <file>${basedir}/my.properties</file>
                            </files>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    <pluginManagement>
        <plugins>
            <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
            <plugin>
                <groupId>org.eclipse.m2e</groupId>
                <artifactId>lifecycle-mapping</artifactId>
                <version>1.0.0</version>
                <configuration>
                    <lifecycleMappingMetadata>
                        <pluginExecutions>
                            <pluginExecution>
                                <pluginExecutionFilter>
                                    <groupId>
                                        org.codehaus.mojo
                                    </groupId>
                                    <artifactId>
                                        properties-maven-plugin
                                    </artifactId>
                                    <versionRange>
                                        [1.0-alpha-2,)
                                    </versionRange>
                                    <goals>
                                        <goal>
                                            read-project-properties
                                        </goal>
                                    </goals>
                                </pluginExecutionFilter>
                                <action>
                                    <ignore></ignore>
                                </action>
                            </pluginExecution>
                        </pluginExecutions>
                    </lifecycleMappingMetadata>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
  </build>
</project>

如果我运行 mvn compile 或 mvn install 结果是:

[ERROR] The build could not read 1 project -> [Help 1]
org.apache.maven.project.ProjectBuildingException: Some problems were encountered while processing the POMs:
[ERROR] 'dependencies.dependency.version' for org.apache.logging.log4j:log4j-api:jar must be a valid version but is   '${log4j.version}'. @ line 16, column 13

“my.properties”文件包含以下内容:

 log4j.version=2.2    

如此处所述:mojohaus

如果我使用 pom.xml 中定义的 prop1 属性,一切正常。

那么我做错了什么?

最佳答案

他们的官方回答是不支持使用 read-project-properties 设置依赖版本:https://github.com/mojohaus/properties-maven-plugin/issues/26

This does not work nor is it the intention of properties maven plugin and furthermore what would be the advantage to read properties from a file to define versions of dependencies?

这个答案对这个问题有很透彻的解释:https://stackoverflow.com/a/14727072/1707491

关于java - 如何使用properties-maven-plugin?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30164134/

相关文章:

java - 混合模式下的 jstack : WrongTypeException: No suitable match for type of address

java - 在交互式命令模式下使用 apache exec

maven - 如何找出在 IntelliJ Idea 中定义了有效 pom 的依赖项的 pom?

java - Maven 分类器空 jar

java - Spring Boot 组件扫描不起作用

java - 是否可以从 maven 编译 grunt 项目?

java - 持久层的命名约定 : DAO vs Manager vs . ..?

java - JLS 6/7 的哪些更改导致以下带有集合和泛型的未经检查的代码在 Java 7 中工作?

java - maven构建成功,但eclipse构建失败

java - 在没有项目的情况下执行 Maven 目标