java 在 WAR 的 WEB-INF/lib 文件夹中重命名 Maven 依赖项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4969313/
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
Renaming Maven dependency in WAR's WEB-INF/lib folder
提问by hleinone
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?
我需要在 Maven 生成的 WARWEB-INF/lib
文件夹中有一个 JAR 依赖项,x-1.0.final.jar
而不是x-1.0.jar
,这是它在存储库中的名称。实现这一目标的最佳方法是什么?
In my POM I have:
在我的 POM 中,我有:
<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
.
我希望它在WEB-INF/lib
文件夹中显示为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.
这是我无法控制的对 Maven Central 的外部依赖。此外,我不想强迫每个人都使用它来将依赖项重新部署到他们的本地存储库。
Is there a Maven plugin that I could utilize or should I start coding my own?
是否有我可以使用的 Maven 插件,或者我应该开始编写自己的代码?
回答by UnclickableCharacter
You can use maven-dependency-plugin
to include artifact under the name that you need.
您可以使用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>
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.
默认情况下,maven-dependency-plugin
绑定到process-sources
似乎刚好适合您的任务的阶段。请记住provided
在依赖项中设置工件的范围,以便war
插件不会自动包含它。
回答by Raghuram
You may want to see if the outputFileNameMappingparameter of maven war plugincan help you.
您可能想看看maven war 插件的outputFileNameMapping参数是否可以帮助您。
回答by Chris
Sorry I dont understand your question fully what is the code you have for importing this jar in the POM?
抱歉,我不完全理解您的问题,您在 POM 中导入这个 jar 的代码是什么?
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.
如果您希望导入的 Jar 在存储库中被调用,并且您在 POM 中导入了正确的文件,那么您不必担心 JAR 文件的命名约定。
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.
我相信您可能需要做的是重命名存储库中的文件,您只需在资源管理器窗口中进入存储库 Users/.m2 并跟踪文件并重命名它即可完成此操作,请注意这可能会对其他项目产生影响.
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
我建议你做的是复制文件重命名并将其添加到具有新工件 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
希望这有助于克里斯
回答by Phil
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:
我知道我正在回复一个旧线程,但我需要执行上述操作并发现该线程很有帮助。我发现实现这一目标的方法是执行两步过程:
- Use the maven-war-plugin to exclude the original jar file from the deliverable.
- 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.
- 使用 maven-war-plugin 从交付中排除原始 jar 文件。
- 使用 maven-dependency-plugin 将原 jar 文件复制到新命名的 jar 文件中,并将其放在 WEB-INF/lib 目录中。
So, by way of illustration, this is how to specify the file you wish to exclude. In this case x-1.0.jar :
因此,作为说明,这是指定要排除的文件的方法。在这种情况下 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':
还指定必须将文件复制到新名称 (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>
In my example I wasn't hard-coding 1.0 but I think this should work for the original posters question.
在我的示例中,我没有对 1.0 进行硬编码,但我认为这应该适用于原始海报问题。