maven通过简单的命令行安装和部署第三方依赖项 [英] maven install and deploy 3rd party dependencies with simple command line

查看:92
本文介绍了maven通过简单的命令行安装和部署第三方依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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文件制作了pom.xml文件,这些文件声明了groupId,artifactId,依赖项等.这些pom.xml文件都具有一个公共的父pom,该pom声明了一些公共信息(例如<repositories><distributionManagement>).

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>).

我希望能够使用mvn installmvn deploy(或者可能是mvn install:install-filemvn deploy:deploy-file)之类的简单内容来安装或部署这些依赖项,并具有这些命令的所有必需属性(artifactIdrepositoryId等)从pom.xml文件中读取.

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

要使其正常工作,至少要进行部署,我尝试将以下内容放入父pom:

To get this to work, at least for deploying, I tried putting the following in my parent 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>

,然后让每个子poms定义jarfile属性.这使我可以运行mvn deploy:deploy-file来部署所有子pom工件.大概我可以做一些类似的事情来使mvn install:install-file正常工作.

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

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

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:perform on the parent pom, I get errors like:

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:

  • 将所有第三方jar的通用代码放在一个共享的父pom中
  • 为每个第三方jar写一个额外的最小pom
  • 无需指定所有复杂的命令行属性即可运行mvn installmvn deploy之类的东西
  • 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 install or mvn deploy without having to specify all those complicated command line properties

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

How can I best accomplish that?

在上面做得更清楚一点,理想情况下,我希望能够运行像mvn installmvn deploy这样简单的内容,而不必在命令行中指定属性. /p>

Made it clearer above that ideally I'd like to be able to run something as simple as mvn install or mvn deploy and not have to specify properties on the command line.

推荐答案

好,我找到了一个解决方案,该解决方案允许我仅运行mvn installmvn deploy并将jar文件安装到本地或远程存储库.受的启发maven用户列表,并使用构建助手插件,在父pom中,我有:

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

<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>

然后在子poms中,我有:

And then in the child poms, I have:

<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:

  • attach-artifact执行应该在<pluginManagement>下,因此如果您mvn installmvn deploy父pom不会被执行.
  • 孩子需要在build插件下指定build-helper-maven-plugin,以便运行来自父<pluginManagement>的代码.
  • 必须将子项声明为具有<packaging>pom</packaging>,因为如果罐子与工件名称相同,则无法将罐子添加到工件.
  • The attach-artifact execution should be under <pluginManagement> so it doesn't get executed if you mvn install or mvn deploy the parent pom.
  • The children need to specify the build-helper-maven-plugin under 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.

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

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

这篇关于maven通过简单的命令行安装和部署第三方依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆