java maven 使用简单的命令行安装和部署 3rd 方依赖项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4632648/
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-10-30 07:18:05  来源:igfitidea点击:

maven install and deploy 3rd party dependencies with simple command line

javamaven-2maven-3

提问by Steve

We have a number of third party dependencies that aren't hosted anywhere. For each of these we have a jar file that we'd like to be able to install and/or deploy to our repository. Some of the jar files have their own dependencies and we also need to declare these.

我们有许多未托管在任何地方的第三方依赖项。对于其中的每一个,我们都有一个 jar 文件,我们希望能够安装和/或部署到我们的存储库。一些 jar 文件有自己的依赖项,我们也需要声明这些。

We've made pom.xml files for each jar file that declare the groupId, artifactId, dependencies, etc. These pom.xml files all have a common parent pom that declares some of the common info (e.g. <repositories>and <distributionManagement>).

我们为每个 jar 文件创建了 pom.xml 文件,声明了 groupId、artifactId、依赖项等。这些 pom.xml 文件都有一个共同的父 pom,它声明了一些共同的信息(例如<repositories><distributionManagement>)。

I'd like to be able to install or deploy these dependencies with something as simple as mvn installand mvn deploy(or maybe mvn install:install-fileand mvn deploy:deploy-file) and have all the necessary properties for these commands (artifactId, repositoryId, etc.) be read from the pom.xml files.

我希望能够用一些简单的安装或部署这些依赖关系mvn installmvn deploy(或者mvn install:install-filemvn deploy:deploy-file)以及对这些命令(所有必要的属性artifactIdrepositoryId等等)从pom.xml的文件中读取。

To get this to work, at least for deploying, I tried putting the following in my parent pom:

为了让它起作用,至少为了部署,我尝试将以下内容放入我的父 pom 中:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-deploy-plugin</artifactId>
      <version>2.4</version>
      <configuration>
        <file>${jarfile}</file>
        <pomFile>pom.xml</pomFile>
        <repositoryId>our-id</repositoryId>
        <url>our-url</url>
      </configuration>
    </plugin>
  </plugins>
</build>

and then having each of the child poms define the jarfileproperty. That allows me to run mvn deploy:deploy-fileto deploy all the child pom artifacts. Presumably I could do something similar to get mvn install:install-fileto work.

然后让每个子 poms 定义jarfile属性。这允许我运行mvn deploy:deploy-file以部署所有子 pom 工件。大概我可以做一些类似的事情mvn install:install-file来上班。

But with this approach, I'm unable to release the parent pom (which I must do since the child poms depend on it), and if I try to mvn release:performon the parent pom, I get errors like:

但是使用这种方法,我无法释放父 pom(我必须这样做,因为子 pom 依赖于它),如果我尝试mvn release:perform在父 pom 上,我会收到如下错误:

Cannot override read-only parameter: pomFile

I feel like I'm probably going about this the wrong way. All I really want to do is:

我觉得我可能会以错误的方式解决这个问题。我真正想做的是:

  • Put the common code for all the third party jars in one shared parent pom
  • Write an additional minimal pom for each third party jar
  • Be able to run something like mvn installor mvn deploywithout having to specify all those complicated command line properties
  • 将所有第三方 jar 的通用代码放在一个共享的父 pom 中
  • 为每个第三方 jar 编写一个额外的最小 pom
  • 能够运行类似mvn installmvn deploy无需指定所有那些复杂的命令行属性的东西

How can I best accomplish that?

我怎样才能最好地做到这一点?

Edit:Made it clearer above that ideally I'd like to be able to run something as simple as mvn installor mvn deployand not have to specify properties on the command line.

编辑:上面说得更清楚,理想情况下,我希望能够运行一些简单的东西,mvn install或者mvn deploy不必在命令行上指定属性。

采纳答案by Steve

Ok, I found a solution that allows me to run just mvn installor mvn deployand have the jar file installed to the local or remote repository. Inspired by a post to the maven-users listand using the build-helper plugin, in the parent pom, I have:

好的,我找到了一个解决方案,它允许我只运行mvn installmvn deploy将 jar 文件安装到本地或远程存储库。受到maven-users 列表中的帖子的启发并使用build-helper 插件,在父 pom 中,我有:

<pluginManagement>
    <plugins>
        <!-- Attach the jar file to the artifact -->
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.5</version>
            <executions>
                <execution>
                    <id>attach-artifacts</id>
                    <phase>package</phase>
                    <goals>
                        <goal>attach-artifact</goal>
                    </goals>
                    <configuration>
                        <artifacts>
                            <artifact>
                                <file>${artifactId}-${version}.jar</file>
                                <type>jar</type>
                            </artifact>
                        </artifacts>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</pluginManagement>

And then in the child poms, I have:

然后在子 poms 中,我有:

<packaging>pom</packaging>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
...
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Some of the pieces of this that initially tripped me up:

最初让我绊倒的一些内容:

  • The attach-artifactexecution should be under <pluginManagement>so it doesn't get executed if you mvn installor mvn deploythe parent pom.
  • The children need to specify the build-helper-maven-pluginunder the build plugins so that code from the parent <pluginManagement>gets run.
  • The children have to be declared as having <packaging>pom</packaging>because you can't add a jar to an artifact if it has the same name as the artifact.
  • attach-artifact执行应当根据<pluginManagement>,因此没有得到,如果你执行mvn installmvn deploy父POM。
  • 子级需要build-helper-maven-plugin在构建插件下指定,以便<pluginManagement>运行父级的代码。
  • 必须将子项声明为具有,<packaging>pom</packaging>因为如果它与工件具有相同的名称,则不能将 jar 添加到工件中。

The only downside I see to this approach is that the artifact gets deployed as type pominstead of type jar. But I haven't seen any real consequences of that.

我认为这种方法的唯一缺点是工件被部署为 typepom而不是 type jar。但我还没有看到任何真正的后果。

回答by Brian Topping

When Maven is missing a dependency, it will give you an error in it's output that contains the command line to issue to add the jar to a remote repository. That command line will automatically create a POM for you and upload the two together. Is that not sufficient for your needs?

当 Maven 缺少一个依赖项时,它会在它的输出中给你一个错误,其中包含将 jar 添加到远程存储库的命令行。该命令行将自动为您创建一个 POM 并将两者一起上传。这还不足以满足您的需求吗?

Using this facility, if I know that I have a dependency with no remote repository representation, I usually just go ahead and define it in my build with the GAV that I want, run the build once, then look at the build errors. Copy out the command for the deployment, then run that command. You'll want to make sure that you have the <repository>elements set up properly in the parent POM plus also set up corresponding <server>elements in your settings.xmlwith the repository upload password. Other than that, you should be good to go.

使用这个工具,如果我知道我有一个没有远程存储库表示的依赖项,我通常只是继续在我的构建中使用我想要的 GAV 定义它,运行一次构建,然后查看构建错误。复制部署命令,然后运行该命令。您需要确保<repository>在父 POM 中正确设置了<server>元素,settings.xml并在您的存储库上传密码中设置了相应的元素。除此之外,你应该很高兴。

I would add that you should check out Nexus before getting to far. It's worth the hassle to set up now! :-)

我想补充一点,你应该在深入了解 Nexus 之前。值得现在设置的麻烦!:-)

回答by Cnetwork

I meet this problem in my work:

我在工作中遇到了这个问题:

Now I have a target.jar (it has a dependencies list : a.jar, b.jar, c.jar...), I want to use mvn install:install-fileto put it into my local repo, but when I run the command blow

现在我有一个 target.jar(它有一个依赖项列表:a.jar、b.jar、c.jar...),我想用mvn install:install-file它把它放到我的本地 repo 中,但是当我运行命令时

mvn install:install-file -Dfile=/Users/username/.../target.jar -DgroupId=com.cnetwork.relevance  -DartifactId=target  -Dversion=1.0.0

but when I use it I found there are many error, the jar which use target.jar cannot find a.jar, b.jar, c.jar, such as:

但是当我使用它时,我发现有很多错误,使用 target.jar 的 jar 找不到a.jar, b.jar, c.jar,例如:

com.cnetwork.a does not exist
com.cnetwork.b does not exist
com.cnetwork.c does not exist

Then I went to ~/.m2/repository/.../target/1.0.0/target.pomto find the pom file of the target, but nothing in it!

然后我去找~/.m2/repository/.../target/1.0.0/target.pom目标的pom文件,但里面什么都没有!

...
<groupId>com.cnetwork.relevance</groupId>
<artifactId>target</artifactId>
<version>1.0.0</version>
....
# no dependencies about a/b/c.jar !!!

This is what going wrong, the install:file -Dfile -DgroupId -D..does not add dependencies into pom, I correct the answer by this method

这是出了什么问题,install:file -Dfile -DgroupId -D..没有将依赖项添加到pom中,我通过这种方法纠正了答案

  1. if you already have this maven project source, just install it into local repo

    mvn clean install

  2. if you have the jar and the pom of it, install jar with pom

    mvn install:install-file -Dfile=/.../.../target.jar -DpomFile=/.../../target.pom

  3. if you don't have a pom with target jar, write one and use upper command.

  4. if you cannot recover the pom file of it, may be you should give up.

  1. 如果你已经have this maven project source,只需将它安装到本地存储库中

    mvn clean install

  2. 如果你have the jar and the pom of it,用 pom 安装 jar

    mvn install:install-file -Dfile=/.../.../target.jar -DpomFile=/.../../target.pom

  3. 如果你don't have a pom with target jar,写一个并使用上层命令。

  4. 如果你不能恢复它的 pom 文件,可能你应该放弃。