In Maven, how output the classpath being used?

MavenClasspathWarMaven War-Plugin

Maven Problem Overview


For my current purposes I have a Maven project which creates a war file, and I want to see what actual classpath it is using when creating the war. Is there a way to do that in a single command -- without having to compile the entire project?

One idea is to have Maven generate the target/classpath.properties file and then stop at that point.

Maven Solutions


Solution 1 - Maven

To get the classpath all by itself in a file, you can:

mvn dependency:build-classpath -Dmdep.outputFile=cp.txt

Or add this to the POM.XML:

<project>
  [...]
  <build>
	<plugins>
	  <plugin>
	    <groupId>org.apache.maven.plugins</groupId>
	    <artifactId>maven-dependency-plugin</artifactId>
	    <version>2.9</version>
	    <executions>
	      <execution>
	        <id>build-classpath</id>
	        <phase>generate-sources</phase>
	        <goals>
	          <goal>build-classpath</goal>
	        </goals>
	        <configuration>
	          <!-- configure the plugin here -->
	        </configuration>
	      </execution>
	    </executions>
	  </plugin>
	</plugins>
  </build>
  [...]
</project>

From: http://maven.apache.org/plugins/maven-dependency-plugin/usage.html

Solution 2 - Maven

This command outputs the classpath on Mac and Linux:

mvn -q exec:exec -Dexec.executable=echo -Dexec.args="%classpath"

Having the result printed and not saved into a file might be useful, for instance, when assigning the result to a variable in a Bash script. This solution runs on Mac and Linux only, but so do Bash shell scripts.

In Windows (e.g. in BAT files), where there is no echo executable, you will need something like this (untested):

mvn -q exec:exec -Dexec.executable=cmd -Dexec.args="/c echo %classpath"

Alternatively, you can just execute java program with the classpath:

mvn -q exec:exec -Dexec.executable=java -Dexec.args="-cp %classpath Main"

Or even like that (it will use the correct classpath automatically):

mvn -q exec:java -Dexec.mainClass="Main" 

However, both these alternative approaches suffer from Maven adding its error messages when your program fails.

Solution 3 - Maven

or call "mvn -e -X ...." and check the output...

Solution 4 - Maven

As ecerulm noted in his comment to Janik's answer, you may want to specify the scope to dependency:build-classpath, as classpath output will differ for different scopes (by default test is used for some reason). I've ended up with a command like this:

mvn -DincludeScope=compile dependency:build-classpath

Within the POM, it could be used like this:

<project>
  [...]
  <build>
	<plugins>
	  <plugin>
	    <groupId>org.apache.maven.plugins</groupId>
	    <artifactId>maven-dependency-plugin</artifactId>
	    <version>2.9</version>
	    <executions>
	      <execution>
	        <id>build-classpath</id>
	        <phase>generate-sources</phase>
	        <goals>
	          <goal>build-classpath</goal>
	        </goals>
	        <configuration>
              <includeScope>compile</includeScope>
              <!-- Omit to print on console: -->
              <outputFile>${project.build.directory}/compile-classpath.txt</outputFile>
	        </configuration>
	      </execution>
	      <execution>
	        <id>build-test-classpath</id>
	        <phase>generate-test-sources</phase>
	        <goals>
	          <goal>build-classpath</goal>
	        </goals>
	        <configuration>
              <includeScope>test</includeScope>
              <!-- Omit to print on console: -->
              <outputFile>${project.build.directory}/test-classpath.txt</outputFile>
	        </configuration>
	      </execution>
	    </executions>
	  </plugin>
	</plugins>
  </build>
  [...]
</project>

This will output 2 versions of classpath, one for main build and the other for tests.

Solution 5 - Maven

The command mvn dependency:list will list the classpath with all the jars used for compilation, runtime and test in the following format:

INFO] --- maven-dependency-plugin:2.8:list (default-cli) @ MyProject ---
[INFO]
[INFO] The following files have been resolved:
[INFO]    org.netbeans.api:org-openide-filesystems:jar:RELEASE80:compile
[INFO]    org.netbeans.api:org-netbeans-modules-queries:jar:RELEASE80:compile
[INFO]    org.netbeans.api:org-netbeans-api-progress:jar:RELEASE80:compile
[INFO]    org.netbeans.api:org-openide-dialogs:jar:RELEASE80:compile
[INFO]    org.apache.derby:derby:jar:10.11.1.1:compile
[INFO]    org.netbeans.api:org-openide-windows:jar:RELEASE80:compile

The only requirement is that the compilation is finished. It doesn't work if the compilation isn't ran.

An other command is The command mvn dependency:tree.

Solution 6 - Maven

Added the missing arg for scope to Janiks answer. Now it is complete. You are welcome.

mvn -q exec:exec -Dexec.classpathScope="compile" -Dexec.executable="echo" -Dexec.args="%classpath" 

Solution 7 - Maven

This is a single command solution but does compile the code.

mvn -e -X -Dmaven.test.skip=true clean compile | grep -o -P '\-classpath .*? ' | awk '{print $2}'

Shell script example usage

MAVEN_CLASSPATH=$(mvn -e -X -Dmaven.test.skip=true clean compile | grep -o -P '\-classpath .*? ' | awk '{print $2}')

I used a variation of it in a shell script to run a standalone main() (for Hibernate schema generation) this way

#/bin/bash

MAVEN_TEST_CLASSPATH=$(mvn -e -X clean package | grep -o -P '\-classpath .*?test.*? ')

java $MAVEN_TEST_CLASSPATH foo.bar.DBSchemaCreate

File output example

mvn -e -X -Dmaven.test.skip=true clean compile | grep -o -P '\-classpath .*? ' | awk '{print $2}' > maven.classpath

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexander BirdView Question on Stackoverflow
Solution 1 - MavenJanik ZikovskyView Answer on Stackoverflow
Solution 2 - MavenDanila PiatovView Answer on Stackoverflow
Solution 3 - MavenPhilip HelgerView Answer on Stackoverflow
Solution 4 - MavenPavel S.View Answer on Stackoverflow
Solution 5 - MavenPaul GuralivuView Answer on Stackoverflow
Solution 6 - MavenmjsView Answer on Stackoverflow
Solution 7 - MavenElMesaView Answer on Stackoverflow