[英]Renaming Maven dependency in WAR's WEB-INF/lib folder
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. 我需要在Maven生成的WAR的
WEB-INF/lib
文件夹中有一个JAR依赖项x-1.0.final.jar
而不是x-1.0.jar
,这是它在存储库中的名称。 What would be the best way to achieve this? 实现这一目标的最佳方法是什么?
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
. 我希望它以
x-1.0.final.jar
出现在WEB-INF/lib
文件夹中。
It's and external dependency on Maven Central I don't have control over. 这是对Maven Central的外部依赖我无法控制。 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? 是否有我可以使用的Maven插件或者我应该开始编写自己的插件?
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. 默认情况下,
maven-dependency-plugin
绑定到process-sources
阶段,这似乎足以完成您的任务。 Remember to set the scope provided
for the artifact in dependencies so that it is not automatically included by the war
plugin. 请记住在依赖项中
provided
为工件provided
的范围,以便war
插件不会自动包含该范围。
您可能想看看maven war插件的outputFileNameMapping参数是否可以帮助您。
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:
我发现实现这一目标的方法是执行两个步骤:
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. 还要指定必须将文件的副本执行到新名称(x-1.0.final.jar),但这需要在打包之前运行。 This is specified by the phase: 'prepare-package':
这由阶段指定:'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,但我认为这应该适用于原始的海报问题。
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. 我相信您可能需要做的是重命名存储库中的文件,只需在资源管理器窗口中进入存储库用户/ .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 希望这有助于克里斯
Just to add to the above answer. 只是添加到上面的答案。 This solution will work only if dependency plugin is configured outside of pluginManagement tag under build tag in your pom.xml
只有在pom.xml中的 build标记下的pluginManagement标记之外配置依赖项插件时,此解决方案才有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.