java - Maven : Unable to get Property Value from Proprties file

标签 java maven properties maven-3

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

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

最佳答案

显示的 POM 将 properties-maven-plugin 执行绑定(bind)到 initialize 阶段,并且 antrun 插件目标显示绑定(bind)到 validate< 的属性 阶段。 initializevalidate 之后(根据 Maven lifecycle docs ),这就是为什么你没有看到你想要的结果。您可以通过以下几种方式进行修复:

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

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

关于java - Maven : Unable to get Property Value from Proprties file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16519690/

相关文章:

c# - 通过属性及其值获取属性名称

java - 在单独的进程中执行 Java 应用程序

java - 在Java中为24小时时间添加冒号?

java - 无法从解压的源存档在 Windows 上运行 mvn 命令

java - Maven 在 "mvn site"期间抛出错误,但在 "mvn compile"/"mvn test"下工作正常

javafx - 将 CheckBox 列添加到现有 TableView

java - 如何在 Spring Controller 中检索 FORM/POST 参数?

java - 如何在接口(interface)默认方法中对新对象创建进行单元测试?

java - SLF4J:线程 "main"java.lang.NoSuchMethodError 中的异常

java - EHCache 禁用/关闭