Leonel
Leonel

Reputation: 29189

Maven: use dependencies from repository when running command line app?

I've used Maven to build my command line application. Now I'm going to distribute it as a jar file, and I need to handle the app's dependencies.

I don't want to include all dependencies in the jar file as described here.

The environment where my app will be run has Maven. I'd like Maven to run my jar looking at file META-INF/groupId/artifactId/pom.xml inside the package so it knows what the dependencies are and can find them in the repository.

Any ideas ?

Upvotes: 4

Views: 2393

Answers (2)

Dev Anand Sadasivam
Dev Anand Sadasivam

Reputation: 753

We can use, maven-jar-plugin instead, why because the classpath generated is not getting accommodated while copy paste with java command in command-line.

mvn -f /temp/tempfile.xml dependency:resolve dependency:build-classpath -DmdepoutputFile=/temp/classpath.txt

So wasn't able to succeed copying classpath.txt for the command,

java -cp yourjar.jar;<created classpath>

Mine is spring-boot application hence I have the following line with BOOT-INF/lib. For you it can be WEB-INF/lib in case of .war file or just lib/ in case of ant build based projects.

<classpathLayoutType>custom</classpathLayoutType>
<customClasspathLayout>BOOT-INF/lib/$${artifact.artifactId}-$${artifact.version}$${dashClassifier?}.$${artifact.extension}</customClasspathLayout>

BOOT-INF, comes up by spring-boot:repackage maven command and with the use of plugin,-spring-boot-maven-plugin that I have not Included here.

Please find maven-jar-plugin config here.

     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <mainClass>com.pakage.SampleApplication</mainClass>
                    <!--<classpathPrefix>lib/</classpathPrefix>-->                          
                    <classpathLayoutType>custom</classpathLayoutType>
                    <customClasspathLayout>BOOT-INF/lib/$${artifact.artifactId}-$${artifact.version}$${dashClassifier?}.$${artifact.extension}</customClasspathLayout>
                    <!--<customClasspathLayout>BOOT-INF/lib/$${artifact.groupIdPath}/$${artifact.artifactId}-$${artifact.version}$${dashClassifier?}.$${artifact.extension}</customClasspathLayout>-->                      
                </manifest>
            </archive>
        </configuration>
    </plugin>

Upvotes: 0

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298818

Include a main class in the jar that 1) extracts the pom to a temporary file, and 2) launches a new maven process using this file with the -f parameter and the goals dependency:resolve and dependency:build-classpath

like this:

mvn -f /temp/tempfile.xml dependency:resolve dependency:build-classpath -DoutputFile=/temp/classpath.txt

then 3) reads the newly created classpath file and 4) launches a new java process using the new classpath file

java -cp yourjar.jar;<created classpath>

Your pom.xml will have to include all required repository information, of course

Upvotes: 6

Related Questions