1

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 for 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?

3
  • To contextualise: I am trying to automate the loading of (sub)project artifacts for Pax Exam integration testing. I need to load them from "bare" filesystem paths, since installation is further down in Maven lifecycle. Commented Sep 19, 2011 at 8:15
  • What part of the target folder name is not portable? The directory separator part, or the fact that the project's output directory can be changed in the project POM? Commented Sep 19, 2011 at 8:31
  • 1
    I was referring to the project output directory being customisable. Commented Sep 19, 2011 at 8:35

1 Answer 1

1

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> 
Sign up to request clarification or add additional context in comments.

4 Comments

This is a fine general solution, since it allows to import custom properties. I noticed that properties-maven-plugin also abilitates this specific approach. However, since the information I require already lies somewhere in the POM, defining additional properties seems redundant to me.
${project.build.outputDirectory} which results in the path to your "target/classes" directory won't help you? That's one of the maven properties which were "behind" that first link I posted.
Yes, more precisely, ${project.build.directory} would. That is not the point. From the second link you provided, it is not clear whether I can build an additional Manifest, thus not polluting the one that will be ultimately packaged. The information that I need is for tests.
I tried that myself - creating or modifying an additional Manifest, but wasn't successful. And I also found no clean way to read information from the MANIFEST.MF file of the war package from my java code. That's the reason for the above solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.