开发者

Refer to Maven project paths from Java

开发者 https://www.devze.com 2023-04-05 14:58 出处:网络
I need to programmatically gather some paths inside a Maven project, in particular to refer to the project artifact.

I need to programmatically gather some paths inside a Maven project, in particular to refer to the project artifact. Using

URL MyClass.class.getClassLoader().getResource(String name)

works f开发者_如何学编程or a path relative to the target/classes folder of the project, but since the artifact sits in the target folder it is not possible to reference it. A path like

System.getProperty("user.dir") + "/target"

does not convince me at all at least for the fact that the target folder name, while standard, is not safely portable.

Is there a Maven-aware library solution that exploits a relative path?


MavenProperties can be written to a manifest file using the maven archiver which is used by the maven war plugin or the maven jar plugin.

If you have a web app, then you can pass some information to the web.xml file, too.

This is an example of one of my projects:

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>
0

精彩评论

暂无评论...
验证码 换一张
取 消