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

醉酒当歌 提交于 2019-12-10 17:47:37

问题


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 ?


回答1:


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




回答2:


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>


来源:https://stackoverflow.com/questions/2943404/maven-use-dependencies-from-repository-when-running-command-line-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!