[英]Refer to Maven project paths from Java
我需要以编程方式收集Maven项目内的一些路径,尤其是要引用项目工件。 使用
URL MyClass.class.getClassLoader().getResource(String name)
适用于相对于项目的target / classes文件夹的路径,但是由于工件位于目标文件夹中,因此无法引用它。 一条像
System.getProperty("user.dir") + "/target"
至少对于目标文件夹名称(虽然是标准的)不能安全地移植这一事实,我至少没有说服我。
是否存在可利用相对路径的Maven感知库解决方案?
可以使用Maven存档程序或Maven jar插件使用的Maven存档器将MavenProperties写入清单文件。
如果您有一个Web应用程序,那么您也可以将一些信息传递到web.xml文件。
这是我的一个项目的示例:
from pom.xml:
------------------------------------------------
<properties>
<maven.build.timestamp.format>dd.MM.yyyy' 'HH:mm:ss</maven.build.timestamp.format>
<build-version>${env.SVN_REVISION}</build-version>
<build-date>${maven.build.timestamp}</build-date>
</properties>
.
.
.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<webResources>
<webResource>
<directory>${basedir}/src/main/webapp/WEB-INF</directory>
<includes>
<include>web.xml</include>
</includes>
<targetPath>WEB-INF</targetPath>
<filtering>true</filtering>
</webResource>
</webResources>
from web.xml:
------------------------------------------------
<context-param>
<param-name>BUILD_VERSION</param-name>
<param-value>${build-version}</param-value>
</context-param>
<context-param>
<param-name>BUILD_DATE</param-name>
<param-value>${build-date}</param-value>
</context-param>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.