Java 如何使用properties-maven-plugin?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30164134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:15:02  来源:igfitidea点击:

How to use properties-maven-plugin?

javamaven

提问by zsom

I'd like to use properties-maven-plugin. I read the usage http://mojohaus.org/properties-maven-plugin/usage.html, but it's not working for me.

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

I created a very simple project to test it. Here is my pom.xml:

我创建了一个非常简单的项目来测试它。这是我的 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>

If I run mvn compile, or mvn install the result is:

如果我运行 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

The "my.properties" file contains this:

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

 log4j.version=2.2    

As it described here: mojohaus

正如这里所描述的:mojohaus

If I use the prop1 property, which is defined in the pom.xml, everything is working.

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

So what I'm doing wrong?

那么我做错了什么?

采纳答案by Quaestor Lucem

Official answer from them is that using read-project-properties for setting version of dependencies is not supported: https://github.com/mojohaus/properties-maven-plugin/issues/26

他们的官方回答是不支持使用 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?

这不起作用,也不是属性 maven 插件的意图,此外,从文件中读取属性以定义依赖项的版本有什么好处?

There is a very through explanation about this issue on this answer: https://stackoverflow.com/a/14727072/1707491

在这个答案上有一个关于这个问题的非常透彻的解释:https: //stackoverflow.com/a/14727072/1707491

回答by Uwe Allner

You have to define the version properties when you want to use them. The plugin just allows you to do so; but you will have to set the values for yourself. Like

当您想使用它们时,您必须定义版本属性。该插件仅允许您这样做;但您必须为自己设置这些值。喜欢

<properties>
    <log4j.version>1.2.17</log4j.version>
</properties>

EDIT:

编辑:

As you showed, you have done that. Now it seems, that as you defined the goal within an execution, it is only valid within that execution. Try to declare it as global:

正如你所展示的,你已经做到了。现在看来,当您在执行中定义目标时,它仅在该执行中有效。尝试将其声明为全局:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-2</version>
    <configuration>
        <files>
            <file>${basedir}/my.properties</file>
        </files>
    </configuration>
...

回答by Rajesh Goel

Make sure that you have ${basedir}/my.properties file present with that specific property value pair: my.properties contents should be: prop1=2.3

确保您有 ${basedir}/my.properties 文件,其中包含该特定属性值对:my.properties 内容应为:prop1=2.3

回答by Charlie Reitzel

The maven way is to use the element in a top level POM that includes the projects with common dependencies. That is the simplest approach.

Maven 方法是在包含具有公共依赖项的项目的顶级 POM 中使用该元素。这是最简单的方法。

You can also import a POM used expressly for dependency managment. The import approach does not require the parent/child project relationships.

您还可以导入专门用于依赖项管理的 POM。导入方法不需要父/子项目关系。

See the Maven documentation:

请参阅 Maven 文档:

  1. Dependency Management
  2. Importing Dependencies
  1. 依赖管理
  2. 导入依赖