重命名 WAR 的 WEB-INF/lib 文件夹中的 Maven 依赖项

发布于 2024-10-17 06:08:05 字数 552 浏览 3 评论 0原文

我需要在 Maven 生成的 WAR 的 WEB-INF/lib 文件夹中有一个 JAR 依赖项 x-1.0.final.jar 而不是 x-1.0.jar,这是它在存储库中的名称。实现这一目标的最佳方法是什么?

在我的 POM 中,我有:

<dependency>
  <groupId>foo</groupId>
  <artifactId>x</artifactId>
  <version>1.0</version>
</dependency>

我希望它以 x-1.0.final.jar 的形式出现在 WEB-INF/lib 文件夹中。

它和对 Maven Central 的外部依赖我无法控制。另外,我不想强​​迫每个使用它的人将依赖项重新部署到他们的本地存储库。

是否有我可以使用的 Maven 插件,或者我应该开始编写自己的插件?

I need to have a JAR dependency in the Maven generated WAR's WEB-INF/lib folder as x-1.0.final.jar instead of x-1.0.jar, which is the name it has in the repository. What would be the best way to achieve this?

In my POM I have:

<dependency>
  <groupId>foo</groupId>
  <artifactId>x</artifactId>
  <version>1.0</version>
</dependency>

I want this to appear in the WEB-INF/lib folder as x-1.0.final.jar.

It's and external dependency on Maven Central I don't have control over. Also I don't want to force everyone using this to redeploy the dependency to their local repositories.

Is there a Maven plugin that I could utilize or should I start coding my own?

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

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

发布评论

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

评论(4

听,心雨的声音 2024-10-24 06:08:05

您可以使用 maven-dependency-plugin 将工件包含在您需要的名称下。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
      <execution>
        <goals>
          <goal>copy</goal>
        </goals>
        <configuration>
          <artifactItems>
            <artifactItem>
              <groupId>foo</groupId>
              <artifactId>x</artifactId>
              <version>1.0</version>
              <type>jar</type>
              <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/lib</outputDirectory>
              <destFileName>x-1.0.final.jar</destFileName>
            </artifactItem>
          </artifactItems>
        </configuration>
      </execution>
    </executions>
</plugin>

默认情况下,maven-dependency-plugin 绑定到 process-sources 阶段,这似乎足以满足您的任务。请记住为依赖项中的工件设置 provided 范围,以便 war 插件不会自动包含它。

You can use maven-dependency-plugin to include artifact under the name that you need.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
      <execution>
        <goals>
          <goal>copy</goal>
        </goals>
        <configuration>
          <artifactItems>
            <artifactItem>
              <groupId>foo</groupId>
              <artifactId>x</artifactId>
              <version>1.0</version>
              <type>jar</type>
              <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/lib</outputDirectory>
              <destFileName>x-1.0.final.jar</destFileName>
            </artifactItem>
          </artifactItems>
        </configuration>
      </execution>
    </executions>
</plugin>

By default, maven-dependency-plugin is bound to the process-sources phase that seems just enough for your task. Remember to set the scope provided for the artifact in dependencies so that it is not automatically included by the war plugin.

呆橘 2024-10-24 06:08:05

您可能想查看 outputFileNameMapping maven war 插件 的参数可以帮助你。

You may want to see if the outputFileNameMapping parameter of maven war plugin can help you.

痴梦一场 2024-10-24 06:08:05

我知道我正在回复旧线程,但我需要执行上述操作并发现该线程很有帮助。我发现实现此目的的方法是执行两步过程:

  1. 使用 maven-war-plugin 从可交付成果中排除原始 jar 文件。
  2. 使用maven-dependency-plugin将原始jar文件复制到新命名的jar文件中,并将其放置在WEB-INF/lib目录中。

因此,作为说明,这就是指定要排除的文件的方法。在本例中 x-1.0.jar :

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <!-- Exclude file: x-1.0.jar from packaging -->
        <packagingExcludes>WEB-INF/lib/x-1.0.jar</packagingExcludes>
    </configuration>
</plugin>

还指定必须将文件复制到新名称 (x-1.0.final.jar),但这需要在打包发生之前运行。这是由阶段指定的:'prepare-package':

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <phase>prepare-package</phase>
            <goals>
                <goal>copy</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>the group of the x jar</groupId>
                        <artifactId>x</artifactId>
                        <type>jar</type>
                        <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/lib</outputDirectory>
                        <destFileName>x-1.0.final.jar</destFileName>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>

在我的示例中,我没有硬编码 1.0,但我认为这应该适用于原始海报问题。

I know that I'm replying to an old thread, but I needed to perform the above and found this thread helpful. The way I found to achieve this was to perform a 2 step process:

  1. Use the maven-war-plugin to exclude the original jar file from the deliverable.
  2. Use the maven-dependency-plugin to copy the original jar file to the new-named jar file and place this in the WEB-INF/lib directory.

So, by way of illustration, this is how to specify the file you wish to exclude. In this case x-1.0.jar :

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.6</version>
    <configuration>
        <!-- Exclude file: x-1.0.jar from packaging -->
        <packagingExcludes>WEB-INF/lib/x-1.0.jar</packagingExcludes>
    </configuration>
</plugin>

Also specify that a copy must be performed of the file to the new name (x-1.0.final.jar) but this needs to run BEFORE packaging occurs. This is specified by the phase: 'prepare-package':

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <phase>prepare-package</phase>
            <goals>
                <goal>copy</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>the group of the x jar</groupId>
                        <artifactId>x</artifactId>
                        <type>jar</type>
                        <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/lib</outputDirectory>
                        <destFileName>x-1.0.final.jar</destFileName>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>

In my example I wasn't hard-coding 1.0 but I think this should work for the original posters question.

独闯女儿国 2024-10-24 06:08:05

抱歉,我不完全理解你的问题,你在 POM 中导入这个 jar 的代码是什么?

如果您希望导入的 Jar 在存储库中被称为该 Jar,并且您正在 POM 中导入正确的文件,那么您不必担心 JAR 文件的命名约定。

我相信您可能需要做的是重命名存储库中的文件,您只需在资源管理器窗口中进入 Repository Users/.m2 并跟踪文件并重命名即可完成此操作,请注意,这可能会对其他项目产生影响。

我建议您做的是复制文件重命名并将其添加到存储库中,并使用新的工件 ID x-1.0.final.jar

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

填写 <>

希望这有帮助
克里斯

Sorry I dont understand your question fully what is the code you have for importing this jar in the POM?

If the Jar you wish to import is called that in the repository and you are importing the correct file in your POM then you shouldn't have to worry about naming conventions of the JAR file.

What i believe you may have to do is rename the file in the repository you can do this simply by going into the Repository Users/.m2 in a explorer window and tracking down the file and renaming it, note this may have implications on other projects.

What i suggest you do is copy the file rename it and add it to the repository with the new artifact id x-1.0.final.jar

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

fill in the <>

Hope this helps
Chris

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