【问题标题】:Maven : Unable to get Property Value from Proprties fileMaven:无法从属性文件中获取属性值
【发布时间】:2013-05-07 08:20:27
【问题描述】:

我是 apache Maven 的新手。当我尝试从属性文件中读取值时,它没有选择值。我已经在 SO 中看到了所有之前提出的问题。但没有运气。这是我的属性文件。

nameofmayil=mayilsamy

这是 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>com.mycompany.mayil</groupId>
  <artifactId>mayil-app</artifactId>
  <version>1.0.1</version>
  <packaging>jar</packaging>

  <name>mayil-app</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>    
      <dependency>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0-alpha-2</version>
      </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>


    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.6</version>
    </dependency>
  </dependencies>

  <scm>
    <connection>scm:svn:http://127.0.0.1/svn/my-project</connection>
    <developerConnection>scm:svn:https://127.0.0.1/svn/my-project</developerConnection>
    <tag>HEAD</tag>
    <url>http://127.0.0.1/websvn/my-project</url>
  </scm>

    <build>
        <plugins>

            <plugin>
                  <groupId>org.codehaus.mojo</groupId>
                  <artifactId>properties-maven-plugin</artifactId>
                  <version>1.0-alpha-2</version>
                  <executions>
                    <!-- Associate the read-project-properties goal with the initialize phase, to read the properties file. -->
                    <execution>
                      <phase>initialize</phase>
                      <goals>
                        <goal>read-project-properties</goal>
                      </goals>
                      <configuration>
                        <files>
                          <file>${basedir}/buildNumber.properties</file>
                          <file>${basedir}/mayil.properties</file>
                        </files>
                        <quite>false</quite>
                      </configuration>
                    </execution>
                  </executions>
                </plugin>

                <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-antrun-plugin</artifactId>
                  <version>1.6</version>
                  <executions>
                    <execution>
                      <phase>validate</phase>
                      <goals>
                        <goal>run</goal>
                      </goals>
                      <configuration>
                        <target>
                          <echo>Displaying value of properties</echo>
                          <echo>${nameofmayil}</echo>
                        </target>
                      </configuration>
                    </execution>
                  </executions>
                </plugin>
          </plugins>
      </build>
</project>

属性文件已正确加载,我检查过。

【问题讨论】:

  • 你能显示构建日志吗?你使用什么命令来运行构建?
  • 好的,运行mvn install 得到什么输出?请将此信息添加到问题中。
  • 在验证阶段运行 write-project-properties,然后您将能够看到所有设置的属性。我真的不记得这个插件是如何工作的,但它可能在属性名称上设置了一些前缀

标签: java maven properties maven-3


【解决方案1】:

显示的 POM 具有绑定到 initialize 阶段的 properties-maven-plugin 执行,以及显示绑定到 validate 阶段的属性的 antrun 插件目标。 initialize 出现在 validate 之后(根据 Maven lifecycle docs),这就是为什么您看不到想要的结果的原因。您可以通过以下几种方式修复:

  • properties-maven-plugin 目标绑定到validate 阶段
  • maven-antrun-plugin 目标绑定到initialize 阶段

以上两个目标都绑定到同一阶段。这很好,只要目标按照您希望它们执行的顺序在 POM 中列出。如果您希望它们绑定到不同的阶段,请执行这两个步骤。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    • 2013-02-08
    • 1970-01-01
    • 1970-01-01
    • 2015-07-04
    相关资源
    最近更新 更多