14

I'd like to set a property in my pom to a classpath containing all the project's dependencies. The ant plugin does something like this, so I know it's definitely possible.

I basically want to use ${maven.compile.classpath} wherever I like in my pom and have it 'just work'. I don't mind using plugins or anything else to achieve this.

Many thanks,

Nick

5 Answers5

11

Since version 2.7 the maven-dependency-plugin can now set a property to the classpath. Here's an example:

  <plugin>
      <artifactId>maven-dependency-plugin</artifactId>
      <version>2.8</version>
      <executions>
          <execution>
              <phase>generate-sources</phase>
              <goals>
                  <goal>build-classpath</goal>
              </goals>
              <configuration>
                <outputProperty>maven.compile.classpath</outputProperty>
                <pathSeparator>;</pathSeparator>
              </configuration>
          </execution>
      </executions>
  </plugin>

If you want Eclipse support here's my update site:

http://terraframe.github.io/m2e-maven-dependency-plugin/snapshots/

Ring
  • 2,249
  • 5
  • 27
  • 40
6

This is how it works:

<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <version>1.7</version>
  <executions>
    <execution>
      <id>define-classpath</id>
      <phase>process-resources</phase>
      <goals>
        <goal>run</goal>
      </goals>
      <configuration>
        <exportAntProperties>true</exportAntProperties>
        <target>
          <property name="maven.classpath" refid="maven.runtime.classpath"/>
        </target>
      </configuration>
    </execution>
  </executions>
</plugin>

After it's executed you can use ${maven.classpath} property.

Betlista
  • 10,327
  • 13
  • 69
  • 110
yegor256
  • 102,010
  • 123
  • 446
  • 597
  • I like this solution a lot, but because there is no antrun m2e connector I'm unable to use it. – Ring Jun 03 '13 at 19:01
  • This doesn't seem to work on parent pom declaration to be used by child poms .... – mjs Dec 29 '17 at 23:29
6

I don't think that there's a way of doing this without writing your own maven plugin. That said, you can get at the classpath using dependency:build-classpath. Is that of use?

Dominic Mitchell
  • 11,861
  • 4
  • 29
  • 30
2

I second the dependency:build-classpath suggestion. It won't put it into a property currently but could easily be modified to do so. (patches accepted)

Brian Fox
  • 6,782
  • 4
  • 27
  • 31
  • http://stackoverflow.com/questions/849389/how-to-read-an-external-properties-file-in-maven discusses how to load the result as a Maven property. An attachment in http://jira.codehaus.org/browse/MCOMPILER-97 offers a complete example. – Jesse Glick Jul 07 '11 at 16:09
0

If you need to generate the classpath as a simple list of jars (without the full path), you can implement a plugin like the one in the example below. My need is to add the classpath in the Manifest using a property other than "Class-Path" because I'm using a tool like Eclipse "JarRsrcLoader" (it's similar to One-JAR) and I want to create a Manifest.MF like this:

Manifest-Version: 1.0
Rsrc-Class-Path: ./ ssm-core-0.0.1-SNAPSHOT.jar commons-codec-1.9.jar 
 commons-io-2.4.jar ehcache-2.8.3.jar spring-beans-4.0.5.RELEASE.jar s
 sm-standalone-cryptlayer-0.0.1-SNAPSHOT.jar shiro-core-1.2.3.jar comm
 ons-beanutils-1.8.3.jar bcprov-jdk15on-1.50.jar javacsv-2.0.jar ssm-f
 ile-persistence-0.0.1-SNAPSHOT.jar spring-context-4.0.5.RELEASE.jar s
 pring-aop-4.0.5.RELEASE.jar aopalliance-1.0.jar spring-core-4.0.5.REL
 EASE.jar commons-logging-1.1.3.jar spring-expression-4.0.5.RELEASE.ja
 r slf4j-log4j12-1.7.7.jar slf4j-api-1.7.7.jar log4j-1.2.17.jar
Built-By: ctasso
Build-Jdk: 1.7.0_10
Class-Path: .

So, I defined a Maven plugin like this:

public void execute() throws MojoExecutionException, MojoFailureException {
        try {


            MavenArchiver mavenArchiver = new MavenArchiver();

            ManifestConfiguration config = new ManifestConfiguration();
            config.setAddClasspath(true);
            Manifest manifest = mavenArchiver.getManifest(project, config);


            String classPath = manifest.getMainAttributes().getValue("Class-Path");

            getLog().debug(String.format("Setting the classpath property %s to %s",classpathVarName,classPath));

            project.getProperties().put(classpathVarName, classPath);

        } catch (DependencyResolutionRequiredException e) {
            throw new MojoFailureException(e.getMessage());
        } catch (ManifestException e) {
            throw new MojoFailureException(e.getMessage());
        }

    }

Using this plugin, you can define a property which contains the list of jars of the classpath:

<plugin>
    <groupId>it.cineca.plugins</groupId>
    <artifactId>classpath-maven-plugin</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <executions>
        <execution>
            <id>set-classpath</id>
            <phase>package</phase>
            <goals>
                <goal>setcp</goal>
            </goals>
            <configuration>
                <classpathVarName>cineca.classpath</classpathVarName>
            </configuration>
        </execution>
    </executions>   
</plugin>

and use this property wherever you want, for example for creating your custom Manifest.MF:

<archive>
     <manifestEntries>
        <Rsrc-Class-Path>./ ${cineca.classpath}</Rsrc-Class-Path>
        <Class-Path>.</Class-Path>
        <Rsrc-Main-Class>it.cineca.cpd.starter.TestStarter</Rsrc-Main-Class>
        <Main-Class>org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader</Main-Class>
     </manifestEntries>
</archive>
Claudio Tasso
  • 417
  • 5
  • 13