我是 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< 的属性
阶段。 initialize
在 validate
之后(根据 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/