I'm using a maven plugin for install4j in my project, located here. That plugin lets you pass variables to install4j using the <compilerVariables> section. Here's the relevant section of my pom:
<plugin>
<groupId>com.google.code.maven-install4j</groupId>
<artifactId>maven-install4j-plugin</artifactId>
<version>0.1.1</version>
<configuration>
<executable>${devenv.install4jc}</executable>
<configFile>${basedir}/newinstaller/ehd.install4j</configFile>
<releaseId>${project.version}</releaseId>
<attach>false</attach>
<skipOnMissingExecutable>false</skipOnMissingExecutable>
<compilerVariables>
<property>
<name>m2_home</name>
<value>${settings.localRepository}</value>
</property>
</compilerVariables>
</configuration>
</plugin>
The problem is that ${settings.localRepository} is not being substituted with the actual directory when I run the plugin. Here's the command line script that install4j is generating:
[INFO] Running the following command for install4j compile: /bin/sh -c /home/zach/install4j/bin/install4jc --release=9.1-SNAPSHOT --destination="/home/zach/projects/java/ehdtrunk/target/install4j" -D m2_home=${settings.localRepository} /home/zach/projects/java/ehdtrunk/newinstaller/ehd.install4j
Is this the fault of the plugin? If so, what needs to change to allow the substitution to happen?
The following POM just works for me (with Maven 2.2.1):
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.stackoverflow</groupId>
<artifactId>Q2828732</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
...
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>validate</phase>
<configuration>
<tasks>
<echo>${settings.localRepository}</echo>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
And running mvn process-ressources produces the following output:
$ mvn validate
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Q2828732
[INFO] task-segment: [validate]
[INFO] ------------------------------------------------------------------------
[INFO] [antrun:run {execution: default}]
[INFO] Executing tasks
[echo] /home/pascal/.m2/repository
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2 seconds
[INFO] Finished at: Thu May 13 19:28:38 CEST 2010
[INFO] Final Memory: 3M/54M
[INFO] ------------------------------------------------------------------------
Does the above work for you? Are you using M2Eclipse? Could it be related to MNGECLIPSE-299?
The plugin in question accepts a parameter which is a Properties instance. For whatever reason, expressions used to configure Properties instances are not automatically evaluated. I had to change the plugin to use a org.apache.maven.plugin.PluginParameterExpressionEvaluator to evaluate ${settings.localRepository}.
Related
Hallo,
I try to run a main method during my maven build process. Hence, I added the exec-maven-plugin and the following snippet to my pom.xml
<plugin>
<!-- http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/ -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<id>compile-reports</id>
<phase>compile</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>at.xyz.dls.util.JasperReportCompiler</mainClass>
</configuration>
</execution>
</executions>
</plugin>
The main class exists and is also executed, when I call it out of the command line:
mvn exec:java -Dexec.mainClass="at.xyz.dls.util.JasperReportCompiler"
I also tried to create a empty new project to test, if an other depencency in the pom causes the problem, but had no success. In the maven settings.xml in the ~/.m2 folder, there is only the entry for an mirror, but no profiles, or anything else, which could cause a problem.
It just does not start it. When I deleted all my artifacts in my repo under org.codehouse, it did not download the exec-maven-plugin. Only when I did the command line call.
Any hints? Thanks in advance!
edit: I forgot to mention I have used "mvn clean install". so it should have passed the compile phase...
edit:
up to now, I could not solve the problem. Thanks for your answers so far! I will try to give all information once again, and hope, one of you guys, find's the fishy part. I have no idea anymore....
What I did:
Delete the folder org/codehouse/mojo in my maven proxy. just to show, when the dependencies are loaded.
show some java and maven version information
build the project with: mvn -clean install (which should pass the test phase to execute my main method)
execute the main method to show, the exec-maven-plugin is loaded only afterwards and works
As far as I understood, calling "mvn clean install" should pass the test phase (the tests are executed...), hence the exec-maven-plugin should be called, which should execute the main method. Am I right?
D:\Eclipse-3.6.1-JSF\ws\exec-test>java -version java version "1.6.0_21" Java(TM) SE Runtime Environment (build 1.6.0_21-b07) Java HotSpot(TM) Client VM (build 17.0-b17, mixed mode, sharing)
D:\Eclipse-3.6.1-JSF\ws\exec-test>mvn -version Apache Maven 2.2.1 (r801777; 2009-08-06 21:16:01+0200) Java version: 1.6.0_21 Java home: C:\Programme\Java\jdk1.6.0_21\jre Default locale: de_AT, platform encoding: Cp1252 OS name: "windows xp" version: "5.1" arch: "x86" Family: "windows" D:\Eclipse-3.6.1-JSF\ws\exec-test>mvn clean install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - at.test:exec-test:jar:0.0.1-SNAPSHOT
[INFO] task-segment: [clean, install]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}]
[INFO] Deleting directory D:\Eclipse-3.6.1-JSF\ws\exec-test\target
[INFO] [resources:resources {execution: default-resources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 1 source file to D:\Eclipse-3.6.1-JSF\ws\exec-test\target\classes
[INFO] [resources:testResources {execution: default-testResources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] Compiling 1 source file to D:\Eclipse-3.6.1-JSF\ws\exec-test\target\test-classes
[INFO] [surefire:test {execution: default-test}]
[INFO] Surefire report directory: D:\Eclipse-3.6.1-JSF\ws\exec-test\target\surefire-reports
T E S T S
Running at.test.ExecTestTest JUnit Test call: end of junit test.
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.031 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: D:\Eclipse-3.6.1-JSF\ws\exec-test\target\exec-test.jar
[INFO] [install:install {execution: default-install}]
[INFO] Installing D:\Eclipse-3.6.1-JSF\ws\exec-test\target\exec-test.jar to D:\maven.m2\repository\at\test\exec-test\0.0.1-SNAPSHOT\exec-test-0.0.1-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3 seconds
[INFO] Finished at: Thu Mar 10 12:07:08 CET 2011
[INFO] Final Memory: 15M/37M
[INFO] ------------------------------------------------------------------------ D:\Eclipse-3.6.1-JSF\ws\exec-test>mvn exec:java -Dexec.mainClass="at.test.ExecTest"
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'exec'. Downloading: http://pced06.vab.sozvers.at:8080/artifactory/repo/org/codehaus/mojo/exec-maven-plugin/1.2/exec-maven-plugin-1.2.pom 6K downloaded (exec-maven-plugin-1.2.pom) Downloading: http://pced06.vab.sozvers.at:8080/artifactory/repo/org/codehaus/mojo/exec-maven-plugin/1.2/exec-maven-plugin-1.2.jar 35K downloaded (exec-maven-plugin-1.2.jar)
[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - at.test:exec-test:jar:0.0.1-SNAPSHOT
[INFO] task-segment: [exec:java]
[INFO] ------------------------------------------------------------------------
[INFO] Preparing exec:java [WARNING] Removing: java from forked lifecycle, to prevent recursive invocation.
[INFO] No goals needed for project - skipping
[INFO] [exec:java {execution: default-cli}]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7 seconds
[INFO] Finished at: Thu Mar 10 12:07:20 CET 2011
[INFO] Final Memory: 5M/15M
[INFO] ------------------------------------------------------------------------
D:\Eclipse-3.6.1-JSF\ws\exec-test>
now again the modified 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>at.test</groupId>
<artifactId>exec-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<finalName>exec-test</finalName>
<defaultGoal>install</defaultGoal>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<!-- http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/ -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<id>compile-reports</id>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>at.test.ExecTest</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
</dependencies>
The main class:
package at.test;
public class ExecTest {
/**
* #param args
*/
public static void main(String[] args) {
System.out.println("##################################");
}
}
And the JUnit Test Class:
package at.test;
import org.junit.Test;
import at.test.ExecTest;
public class ExecTestTest {
#Test
public void testTestTest() {
System.err.println("JUnit Test call:");
ExecTest.main(null);
System.err.println("end of junit test.");
}
}
Finally I found the problem. Was quite a stupid mistake:
In the pom.xml I used the <pluginManagement> around the <plugins> node.
Maven did not complain, but ignored all plugin configurations.
Maybe someone can use this information...
Thanks for your help!
Compile phase may not be appropriate, have you tried
<phase>test</phase>
If you set it on phase compile then you do have to tell maven that is needs to run compile when running. You don't pass any phase with your maven command. You should have mvn compile exec:java ... or higher (test, install, ...).
This is my usage of the exec maven plugin (with mvn clean package):
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.xxx.LanguageGenerator</mainClass>
<arguments>
<argument>${project.build.outputDirectory}/build/PMLanguage.xls</argument>
<argument>PM${project.version}</argument>
<argument>${project.build.outputDirectory}/com/laco/projectmaster/props/resources</argument>
<argument>Created during maven build (POM Version:
${project.version})</argument>
</arguments>
</configuration>
</plugin>
hey,
I have a deploy pojo plugin (deploying a war to a remote server). I have the remote-deploy plugin in the build section of pom definition, I need package phase to be triggered before deploy-remote goal, for it the war be already created before I secure-copy it to a remote server.
With the execution elements (according to a documentation), I can attach my goal to a particular phase, for instance bind it to the phase after, so in my case, install phase ...but that's just a workaround.
<build>
<plugins>
<plugin>
<groupId>sample.plugin</groupId>
<artifactId>maven-hello-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>sayhi</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
simply put, if I place only my goal into the build section, and run it, package phase is not run before. Please help
Maven Mojo plugin, how to define phases that must be triggered before this goal ?
You can't.
I have the remote-deploy plugin in the build section of pom definition, I need package phase to be triggered before deploy-remote goal, for it the war be already created before I secure-copy it to a remote server.
Just bind it to the package phase, your goal will be called after the goals bounds to package by default (so the war will be there).
Here is an example demonstrating this behavior with the Maven AntRun plugin configured like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>package</phase>
<configuration>
<target>
<echo message="Hi!!!!!"/>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
And the output of mvn package:
$ mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Q3934833 Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
...
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) # Q3934833 ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.5:test (default-test) # Q3934833 ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-war-plugin:2.1:war (default-war) # Q3934833 ---
[INFO] Packaging webapp
[INFO] Assembling webapp [Q3934833] in [/home/pascal/Projects/stackoverflow/Q3934833/target/Q3934833]
[INFO] Processing war project
[INFO] Copying webapp resources [/home/pascal/Projects/stackoverflow/Q3934833/src/main/webapp]
[INFO] Webapp assembled in [317 msecs]
[INFO] Building war: /home/pascal/Projects/stackoverflow/Q3934833/target/Q3934833.war
[INFO] WEB-INF/web.xml already added, skipping
[INFO]
[INFO] --- maven-antrun-plugin:1.6:run (default) # Q3934833 ---
[INFO] Executing tasks
main:
[echo] Hi!!!!!
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
...
The antrun plugin is executed after package, as expected.
You can try use PREPARE_PACKAGE phase in your #Mojo annotation:
#Mojo(name = "myName", defaultPhase = LifecyclePhase.PREPARE_PACKAGE)
I want to create a build info file into the specific location of the project's target folder (especially in target/abc_project/META-INF folder) through maven-2.
Following is what I am doing in the pom.xml
<build>
<finalName>abc_project</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<id>buid-info-generator</id>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>java</executable>
<arguments>
<argument>-jar</argument>
<argument>xyz.jar</argument>
<argument>target/abc_project/META-INF/info.txt</argument>
<argument>date</argument>
<argument>hg summary</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugins>
</build>
while giving the phase other than install, deploy, I get the following error -
[INFO] Exception in thread "main" java.io.FileNotFoundException: target\abc_project\META-INF\info.txt (The system cannot find the path specified)
[INFO] at java.io.FileOutputStream.open(Native Method)
[INFO] at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
[INFO] at java.io.FileOutputStream.<init>(FileOutputStream.java:131)
[INFO] at java.io.FileWriter.<init>(FileWriter.java:73)
[INFO] at com.nbec.svn.build.info.BuildInfoGenerator.main(BuildInfoGenerator.java:30)
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Result of cmd.exe /X /C "java -jar xyz.jar target/abc_project/META-INF/info.txt "date "hg summary"" execution is: '1'.
But strangely, the same code is working for 1 project and not for other 2 projects. Is there any alternative to obtain the same.
Actually the error report indicates that the directory target\abc_project\META-INF probably does not exist. Without more information I can only speculate. Maybe the plugin which create the abc_project\META-INF directory is called after the exec-maven-plugin.
I want to extract all the properties from my pom.xml into a properties-file. These are the common properties like dependency-versions, plugin-versions and directories.
I'm using the properties-maven-plugin, but its not working as i want it to.
The essential part of my pom.xml:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-1</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${basedir}/pom.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
Now when i run "mvn properties:read-project-properties" i get the following error:
[INFO] One or more required plugin parameters are invalid/missing for 'properties:read-project-properties'
[0] Inside the definition for plugin 'properties-maven-plugin' specify the following:
<configuration>
...
<files>VALUE</files>
</configuration>.
The pom.properties-file is located in the same dir as the pom.xml.
What can i do to let the properties-maven-plugin read my properties-file?
EDIT
I filed an issue at http://jira.codehaus.org/browse/MOJO-1523.
It has been closed as "not a bug", the reason is:
It's by design. The project definition
has to be self-contained, otherwise it
is no longer complete if it is refered
from elsewhere as part of the
transitive deps.
Your configuration element is defined inside an execution and thus applies to this execution only.
So either call mvn initialize (or a phase posterior to initialize) to use the configuration of your current execution binding.
Or use a global configuration:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<configuration>
<files>
<file>etc/config/dev.properties</file>
</files>
</configuration>
...
</plugin>
And then call
mvn properties:read-project-properties
But that wouldn't make much sense in the particular case of this plugin (you want the properties to be available during the build) so this leaves you with the first solution.
Update: I did a test on my side and, indeed, with the following POM:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.stackoverflow</groupId>
<artifactId>Q2664362</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>etc/config/dev.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Running mvn test won't work: maven will try to download junit:jar:${junit.version} (i.e. it doesn't use the value of the property) and this will obviously fail.
$ mvn test
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building SO - Q2664362 - maven-properties-plugin demo
[INFO] task-segment: [test]
[INFO] ------------------------------------------------------------------------
[INFO] [properties:read-project-properties {execution: default}]
[INFO] [resources:resources {execution: default-resources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/pascal/Projects/stackoverflow/Q2664362/src/main/resources
Downloading: http://repo1.maven.org/maven2/junit/junit/${junit.version}/junit-${junit.version}.pom
[INFO] Unable to find resource 'junit:junit:pom:${junit.version}' in repository central (http://repo1.maven.org/maven2)
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Nothing to compile - all classes are up to date
[INFO] [resources:testResources {execution: default-testResources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/pascal/Projects/stackoverflow/Q2664362/src/test/resources
Downloading: http://repo1.maven.org/maven2/junit/junit/${junit.version}/junit-${junit.version}.jar
[INFO] Unable to find resource 'junit:junit:jar:${junit.version}' in repository central (http://repo1.maven.org/maven2)
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Failed to resolve artifact.
...
The odd part is that the download of the dependency occurs after properties:read-project-properties. I'm not sure but this sounds like a bug, you should open an issue.
Try using validate phase instead of initialize for maven 3.x (link).
EDIT2
See here for a workaround using Ant Tasks, which makes this use case possible
I encounter your question, but i tried to add this resources here, it works well.
<build>
<resources>
<resource>
<directory>src/config</directory> //your config folder
<filtering>true</filtering>
</resource>
</resources>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>src/config/config.properties</file> //your config file
</files>
</configuration>
</execution>
</executions>
</plugin>
</build>
Hope you resolve this as above
I have a maven dependency in my pom.xml as such:
<dependency>
<groupId>com.foo</groupId>
<artifactId>Bar</artifactId>
<version>1.2.3</version>
</dependency>
And I would like to use the system path to the binary as a property (so I can pass it to an external process that is kicked off by maven). I can do this in an awkward way:
<properties>
<my.lib>${settings.localRepository}/com/foo/Bar/1.2.3/Bar.jar</my.lib>
</properties>
But I would really like to use a more standard mechanism, such as:
<properties>
<my.lib>${com.foo:Bar:1.2.3}</my.lib>
</properties>
I something like that possible?
Here is a correct implementation, using the maven-dependency-plugin properties goal, which can be used anywhere in a pom:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.stackoverflow</groupId>
<artifactId>q2359872</artifactId>
<version>2.0-SNAPSHOT</version>
<name>q2359872</name>
<properties>
<!-- Must be listed in the dependencies section otherwise it will be null. -->
<my.lib>${org.jmockit:jmockit:jar}</my.lib>
</properties>
<dependencies>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>1.11</version>
</dependency>
</dependencies>
<build>
<defaultGoal>generate-sources</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Example usage: -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
<phase>generate-sources</phase>
</execution>
</executions>
<configuration>
<executable>echo</executable>
<arguments>
<argument>path to jar=</argument>
<argument>${org.jmockit:jmockit:jar}</argument>
<argument>my.lib=</argument>
<argument>${my.lib}</argument>
</arguments>
</configuration>
</plugin>
<!-- end of Example usage -->
</plugins>
</build>
</project>
And the output is...
jpyeron#black /projects/wkspc/tmp/foo
$ /cygdrive/c/programs.x86_64/apache-software-foundation/apache-maven-3.1.1/bin/mvn
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building q2359872 2.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:2.3:properties (default) # q2359872 ---
[INFO]
[INFO] --- exec-maven-plugin:1.2:exec (default) # q2359872 ---
path to jar= C:\Documents and Settings\jpyeron\.m2\repository\org\jmockit\jmockit\1.11\jmockit-1.11.jar my.lib= C:\Documents and Settings\jpyeron\.m2\repository\org\jmockit\jmockit\1.11\jmockit-1.11.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.032s
[INFO] Finished at: Wed Sep 17 12:07:18 EDT 2014
[INFO] Final Memory: 10M/153M
[INFO] ------------------------------------------------------------------------
Assuming that the com.foo:Bar:jar:1.2.3 artifact is declared as dependency in your POM, the following property returns the path to the jar in the local repository:
${maven.dependency.com.foo.Bar.jar.path}
Update: Here is a simple POM demonstrating this:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.stackoverflow</groupId>
<artifactId>q2359872</artifactId>
<version>1.0-SNAPSHOT</version>
<name>q2359872</name>
<properties>
<my.lib>${maven.dependency.junit.junit.jar.path}</my.lib>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>process-resources</phase>
<configuration>
<tasks>
<echo>${my.lib}</echo>
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Running mvn process-resources produces the following output:
$ mvn process-resources
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building q2359872
[INFO] task-segment: [process-resources]
[INFO] ------------------------------------------------------------------------
[INFO] [resources:resources {execution: default-resources}]
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/pascal/Projects/stackoverflow/q2359872/src/main/resources
[INFO] [antrun:run {execution: default}]
[INFO] Executing tasks
[echo] /home/pascal/.m2/repository/junit/junit/3.8.1/junit-3.8.1.jar
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7 seconds
[INFO] Finished at: Tue Mar 02 14:41:32 CET 2010
[INFO] Final Memory: 7M/68M
[INFO] ------------------------------------------------------------------------
There is a plugin which might be what you are looking for... bitstrings.org (home).
If none of the upper work, you can always use gmaven to agressively dive into MavenProject object and get your artifact infos. In my case, I had the following artifact declared in a profile :
<!-- Neo4J connector. This dependency is scoped to be usable by maven-exec-plugin
which installs it in Glassfish -->
<dependency>
<groupId>com.netoprise</groupId>
<artifactId>neo4j-connector</artifactId>
<version>${neo4j.connector.version}</version>
<type>rar</type>
<!-- Set in test scope to avoid release issues -->
<scope>test</scope>
</dependency>
To get its path and put it in a maven property, I wrote the following gmaven script :
<!-- Small script used to build maven property for neo4j-connector path -->
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>get-neo4j-connector-rar-path</id>
<phase>validate</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
<![CDATA[
println "initial value of neo4j.connector.rarPath is \""+project.properties['neo4j.connector.rarPath']+"\""
// Duplicate model in a Mavenproject, allowing me to get associated artifact
// So sad I can't get the embdder object
// More info here : http://maven.apache.org/ref/3.0.3/maven-core/apidocs/org/apache/maven/project/MavenProject.html
def mavenProject = new org.apache.maven.project.MavenProject(project)
// More infos on Artifact there : http://maven.apache.org/ref/3.0.3/maven-artifact/apidocs/org/apache/maven/artifact/Artifact.html
def neo4jConnector = mavenProject.getArtifacts().find { artifact -> artifact.getArtifactId()=='neo4j-connector' }
// Now resolve dependency to produce an artifact
// notice maven property interpolation doesn't do toString, so we have to do it ourselves
project.properties['neo4j.connector.rarPath'] = neo4jConnector.getFile().getAbsolutePath()
println "usable neoj4Connector can be found at "+project.properties['neo4j.connector.rarPath']
]]>
</source>
</configuration>
</execution>
</executions>
</plugin>
It's some kind of brute-force method, but it DO work far better than the previous solutions I've seen there.
You need to write a new maven plugin that sets a property value to the fully-resolved pathname of a dependency. The maven-dependency-plugin won't do that for you.
It will copy your dependency somewhere and then you can refer to it by that pathname.