6

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

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

我希望能够使用mvn installmvn deploy(或者可能mvn install:install-file和)这样简单的东西来安装或部署这些依赖项,并从 pom.xml 文件中读取这些命令( 、等)mvn deploy:deploy-file的所有必要属性。artifactIdrepositoryId

为了让它工作,至少在部署时,我尝试在我的父 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>

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

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

Cannot override read-only parameter: pomFile

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

  • 将所有第三方 jar 的通用代码放在一个共享的父 pom 中
  • 为每个第三方 jar 编写一个额外的最小 pom
  • 能够运行类似mvn installmvn deploy不必指定所有那些复杂的命令行属性的东西

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

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

4

3 回答 3

2

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

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

我要补充一点,你应该在走远之前检查一下 Nexus。现在设置的麻烦是值得的!:-)

于 2011-01-08T14:23:15.727 回答
2

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

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

然后在 child 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>

最初让我绊倒的一些部分:

  • attach-artifact执行应该在下面,所以如果你或父 pom.xml<pluginManagement>不会被执行。mvn installmvn deploy
  • 孩子需要build-helper-maven-plugin在构建插件下指定,以便<pluginManagement>运行来自父母的代码。
  • 必须将子项声明为具有,<packaging>pom</packaging>因为如果 jar 与工件具有相同的名称,则不能将其添加到工件中。

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

于 2011-01-09T16:53:40.747 回答
0

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

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

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

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

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

然后我去~/.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 !!!

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

  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文件,可能您应该放弃。

于 2017-11-08T12:32:16.847 回答