Java 从现有 jar 创建 Maven 工件的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2177069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:26:29  来源:igfitidea点击:

Best way to create a maven artifact from existing jar

javamaven-2jar

提问by 3wh

I'm mavenizing some projects.

我正在研究一些项目。

These projects all depend on a number of libraries, most of them are available in the maven repo. For the other libraries, I'd like to create a maven artifact, so I can use it as an dependency. The problem is, I only have jar files of these libraries.

这些项目都依赖于许多库,其中大部分都可以在 maven repo 中找到。对于其他库,我想创建一个 maven 工件,以便我可以将其用作依赖项。问题是,我只有这些库的 jar 文件。

What is the best way to create artifacts from existing jar files?

从现有 jar 文件创建工件的最佳方法是什么?

回答by danben

You can use the Maven Deploy Pluginto upload your JAR file (and optionally a POM file, though by default one will be created for you) to your Maven repository.

您可以使用Maven Deploy Plugin将您的 JAR 文件(以及可选的 POM 文件,尽管默认情况下会为您创建一个)上传到您的 Maven 存储库。

回答by Valentin Rocher

As danben said, you'll have to deploy these jar to a repository. However, I seem to understand from your question that you don't have a repository except the global maven one.

正如danben 所说,您必须将这些 jar 部署到存储库。但是,我似乎从您的问题中了解到,除了全球 maven 之外,您没有其他存储库。

You could use Nexus, or Artifactory.

您可以使用NexusArtifactory

回答by Pascal Thivent

If you're not using a remoterepository (which is a common situation for personal development), simply installthese artifacts in your localrepository using the install:install-filemojo:

如果您不使用远程存储库(这是个人开发的常见情况),只需使用mojoinstall将这些工件放在本地存储库中install:install-file

mvn install:install-file 
  -Dfile=<path-to-file> 
  -DgroupId=<group-id> 
  -DartifactId=<artifact-id> 
  -Dversion=<version> 
  -Dpackaging=<packaging> 
  -DgeneratePom=true

Where: <path-to-file>  the path to the file to load
       <group-id>      the group that the file should be registered under
       <artifact-id>   the artifact name for the file
       <version>       the version of the file
       <packaging>     the packaging of the file e.g. jar

But obviously, this will make your build non portable (this might not be an issue though). To not sacrifice the portability, you'll have to make the artifacts available in a remote repository. In a corporate context, the common way to deal with that is to install an enterprise repository (and in that case, to deploythe artifacts indeed).

但显然,这将使您的构建不可移植(尽管这可能不是问题)。为了不牺牲可移植性,您必须使工件在远程存储库中可用。在企业环境中,处理该问题的常用方法是安装企业存储库(在这种情况下,deploy确实安装到工件)。

Update:Once your artifact is installed in your local repository, simply declare a <dependency>element in your pom like for any other dependency, e.g.:

更新:一旦你的工件安装在你的本地存储库中,只需<dependency>在你的 pom 中声明一个元素,就像任何其他依赖项一样,例如:

<dependency>
  <groupId>aGroupId</groupId>
  <artifactId>aArtifactId</artifactId>
  <version>1.0.12a</version>
  <packaging>jar</packaging>
</dependency>

回答by manuel aldana

I know your problem. Mavenizing jars is sometimes a pain (especially if they have further transitive dependencies, which also need to be defined in pom.xml).

我知道你的问题。Mavenizing jars 有时会很痛苦(特别是如果它们有进一步的传递依赖,也需要在 pom.xml 中定义)。

Have you checked whether these libraries really never exist as maven deps? Have a look at the usual suspects:

您是否检查过这些库是否真的从未作为 maven deps 存在?看看常见的嫌疑人:

Sometimes I like to use Nexus jar upload dialog to let create pom.xml files.

有时我喜欢使用 Nexus jar 上传对话框来创建 pom.xml 文件。

回答by Kirby

You can also create 'system' dependencies on jars that are not in a repository that are in the project. For example,

您还可以对不在项目中存储库中的 jar 创建“系统”依赖项。例如,

    <dependency>
        <groupId>com.example</groupId>
        <artifactId>MySpecialLib</artifactId>
        <version>1.2</version>
        <scope>system</scope>
        <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/MySpecialLib-1.2.jar</systemPath>
    </dependency>