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

发布于 2024-10-11 10:08:24 字数 1766 浏览 1 评论 0原文

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

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

我希望能够使用 mvn installmvn deploy (或者也许 mvn install:install-file< /code> 和 mvn deploy:deploy-file),并具有这些命令的所有必要属性(artifactIdrepositoryId 等)从 pom.xml 文件中读取。

为了让它工作,至少对于部署来说,我尝试将以下内容放入我的父 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依赖于它),如果我尝试在父pom上mvn release:perform,我得到这样的错误:

Cannot override read-only parameter: pomFile

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

  • 将所有第三方 jar 的通用代码放在一个共享父 pom 中
  • 为每个第三方 jar 编写一个额外的最小 pom
  • 能够运行诸如 mvn install 或 < code>mvn deploy 无需指定所有这些复杂的命令行属性

我怎样才能最好地实现这一点?

编辑:上面说得更清楚了,理想情况下我希望能够运行像mvn installmvn deploy这样简单的东西,并且没有在命令行上指定属性。

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.

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

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.

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>

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.

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:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何时共饮酒 2024-10-18 10:08:24

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

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

我想补充一点,在深入讨论之前你应该先检查一下 Nexus。现在就去设置是值得的! :-)

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?

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.xml with the repository upload password. Other than that, you should be good to go.

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

↘紸啶 2024-10-18 10:08:24

好的,我找到了一个解决方案,允许我仅运行 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>

然后在子 pom 中,我有:

<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 执行应该在 下执行。因此,如果您 mvn installmvn deploy 父 pom.pluginManagement> ,它就不会被执行。
  • 子级需要在构建插件下指定 build-helper-maven-plugin ,以便运行父级 中的代码。
  • 子项必须声明为具有 pom 因为如果 jar 与工件同名,则无法将其添加到工件中。

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

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>

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:

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

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.

小女人ら 2024-10-18 10:08:24

我在工作中遇到这样的问题:

现在我有一个 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.jarb.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. 如果您已经有这个maven项目源码,安装到本地repo即可

    mvn clean install

  2. < p>如果您有 jar 及其 pom,请使用 pom 安装 jar

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

  3. 如果您没有带有目标jar的pom,请编写一个并使用upper命令。

  4. 如果你无法恢复它的pom文件,也许你应该放弃。

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-file to put it into my local repo, but when I run the command blow

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:

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.pom to find the pom file of the target, but nothing in it!

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

  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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文