简体   繁体   English

如何创建一个由buildpath使用的依赖项的Maven项目jar?

[英]How to create a Maven project jar with dependencies which are used by the buildpath?

I am trying to create a Maven project jar file. 我正在尝试创建一个Maven项目jar文件。 I know about the Maven repository and the pom.xml file configuration. 我了解Maven信息库和pom.xml文件配置。 But my query is a bit different from that. 但是我的查询与此有所不同。

I have created a Maven repository. 我已经创建了一个Maven存储库。 I have included the respective dependency jar files by downloading them and then given in the build path of the project (as I am using Eclipse Neon). 我已经下载了各自的依赖项jar文件,然后在项目的构建路径中给出了它们(就像我使用Eclipse Neon一样)。

Now when I run as --> Maven install , it creates a jar file. 现在,当我run as --> Maven install时,它将创建一个jar文件。 But that jar is of the only project and not the dependencies that I have included. 但是那个jar是唯一的项目,而不是我包含的依赖项。

I have checked and got suggestions that I need to include them in the Maven pom.xml, but I do not know exactly which jar is getting used in my program. 我已经检查并获得建议,需要将它们包括在Maven pom.xml中,但是我不确切知道程序中正在使用哪个jar。

So I would like to know how can I automatically make the Maven project detect the dependencies and write them to the pom.xml file without my interference, if possible. 因此,我想知道如何在可能的情况下自动使Maven项目检测依赖关系并将其写入pom.xml文件而不会受到干扰。

Kindly let me know how is this possible? 请让我知道这怎么可能?

I have solved this problem using the maven-dependency-plugin . 我已经使用maven-dependency-plugin解决了这个问题。

This plugin make a copy of all dependencies needed by your jar file into the directory ${project.build.directory}/lib . 该插件将jar文件所需的所有依赖项复制到${project.build.directory}/lib目录中。

When you need to start your jar file you have to specify the classpath -cp /path/to/your-jar-file.jar:/path/to/your/lib/* 当您需要启动jar文件时,必须指定类路径-cp /path/to/your-jar-file.jar:/path/to/your/lib/*

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.8</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Another solution can be use directly Maven to start your application, in this way maven transparently add all the dependencies configured. 另一个解决方案可以直接使用Maven启动您的应用程序,以这种方式maven透明地添加所有已配置的依赖项。 You have just to take care before of that to execute the Maven package goal. 在执行Maven package目标之前,您只需要保重。

 # create your jar file with maven
 mvn clean package

 # execute your class where is main 
 mvn exec:java -Dexec.mainClass="your.package.App" -Dexec.args="your app params"

You can even wrap this line into a shell script run.sh : 您甚至可以将此行包装到shell脚本run.sh

#!/bin/bash

PARAMS="$*"

if [ "A$PARAMS" == "A" ]
then
  PARAMS="--help"
fi

mvn exec:java -Dexec.mainClass="your.package.App" -Dexec.args="$PARAMS"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何导出将在另一个Java项目中使用的jar以及jar使用的整个maven /外部依赖关系? - How do I export a jar that will be used in another Java project, along with the entire maven/external dependencies that the jar uses? 如何使用 maven 和系统依赖项创建可执行的 jar? - How to create executable jar with maven and system dependencies? 如何将我的maven项目部署为jar文件,该文件将使用pom文件获取其依赖项? - How to deploy my maven project as a jar file which will take its dependencies using a pom file? 如何从buildpath中的runnable jar文件中获取资源文件 - How to get resource files from the runnable jar file which are in buildpath Maven如何使用jar项目的依赖关系定义为war项目的依赖关系 - Maven How to use dependencies of a jar project defined as a dependency of a war project 如何在 netbeans 中创建 maven 项目的 jar 文件 - how to create a jar file of a maven project in neatbeans 如何在 Gradle 项目中声明特定 Jar Maven 发布的依赖项? - How to declare dependencies for a specific Jar Maven publication in a Gradle project? 在Tomcat7上创建REST服务的依赖关系(带有Maven的Netbeans项目) - Which dependencies to create REST services on Tomcat7 (Netbeans project with Maven) 如何创建仅包含依赖项的Maven jar包 - How to create maven jar package including just dependencies 如何使用Maven创建一个jar,其中每个依赖项在单独的文件夹中包括依赖项 - How to create a jar with Maven that includes dependencies in a separate folder for each dependency
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM