Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile with all .jars in .m2?

I'm trying debug mvn compile of a file with many dependencies with javac.

This is how I'm trying to do this:

CLASSPATH=`find ~/.m2 -name *.jar -print0`; javac -verbose BogusFile.java

My problem is that I'm not sure how to tell find to separate each jar found with the unix file separator (:).

Maybe the -printf has the solution?

like image 869
simpatico Avatar asked Dec 28 '22 20:12

simpatico


2 Answers

Sorry I can not answer your question but give a possible other solution approach.

If you need to build a classpath for your maven project you can run the copy-dependencies goal of the Maven Dependency Plugin on your project:

mvn dependency:copy-dependencies

Maven will copy all dependencies for your project (also transitive) to the target/dependency directory and classpath can be set to target/dependency/*; (you still have to include your artifact jar).

Example:

Code:

import org.apache.commons.lang.WordUtils;
import org.apache.log4j.Logger;

public class Bogus {

    private static final Logger LOG = Logger.getLogger(Bogus.class);

    public static void main(final String[] args) {
        LOG.debug(WordUtils.capitalize("hello world"));
    }
}

Directory:

C:.
│
├───src
│   └───main
│       └───java
│               Bogus.java
│
└───target
    └───dependency
            commons-lang-2.6.jar
            log4j-1.2.16.jar

Compile Command:

.....\bogus>javac -cp target\dependency\*; src\main\java\Bogus.java

Result:

C:.
│
├───src
│   └───main
│       └───java
│               Bogus.class
│               Bogus.java
│
└───target
    └───dependency
            commons-lang-2.6.jar
            log4j-1.2.16.jar
like image 162
FrVaBe Avatar answered Jan 10 '23 14:01

FrVaBe


Join all the jars by : separator, to use as the classpath for the compiler.

export TEST_CLASSPATH=$(find ~/.m2 -name *.jar | sed -z 's/\n/:/g')
javac -classpath $TEST_CLASSPATH:./ BogusFile.java
like image 45
Bluu Avatar answered Jan 10 '23 16:01

Bluu