Nicholas White
Nicholas White

Reputation:

Setting the a Property to what maven.compile.classpath Contains WITHOUT Ant

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

Upvotes: 14

Views: 18256

Answers (5)

Ring
Ring

Reputation: 2309

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/

Upvotes: 11

Claudio Tasso
Claudio Tasso

Reputation: 465

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>

Upvotes: 0

yegor256
yegor256

Reputation: 105210

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.

Upvotes: 6

Brian Fox
Brian Fox

Reputation: 6832

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)

Upvotes: 2

Dominic Mitchell
Dominic Mitchell

Reputation: 12319

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?

Upvotes: 6

Related Questions