0

我是 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>

属性文件已正确加载,我对此进行了检查。

4

1 回答 1

2

显示的 POM 具有绑定到阶段的properties-maven-plugin执行initialize,以及显示绑定到阶段的属性的 antrun 插件目标validateinitialize紧随其后validate(根据 Maven生命周期文档),这就是为什么您看不到所需结果的原因。您可以通过以下几种方式修复:

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

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

于 2013-05-13T16:13:25.437 回答