Java 使用Maven从文件名中删除版本号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2225142/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:55:32  来源:igfitidea点击:

Removing version number from file name with Maven

javabuildmaven-2earmaven-ear-plugin

提问by Bani

I have a project with 5 modules in maven. Right now, when I do a "mvn clean install", it generates an ear containing the two jars, one war and one sar from the other modules.

我在 Maven 中有一个包含 5 个模块的项目。现在,当我执行“mvn clean install”时,它会生成一个包含两个罐子的耳朵,一个是war,一个是来自其他模块的sar。

All the file names contain the version, e.g., projectx-ear-2.0.0.ear, projectx-client-2.0.0.jar, etc.

所有文件名都包含版本,例如projectx-ear-2.0.0.ear、projectx-client-2.0.0.jar等。

I need to rename one of the jars and the final ear to omit the version in the file name. It should look like this:

我需要重命名其中一个罐子和最后一个耳朵以省略文件名中的版本。它应该是这样的:

projectx-ear.ear
|
+-- projectx-client.jar
|
+-- projectx-misc-2.0.0.jar
|
+-- projectx-sar-2.0.0.sar
|
\-- projectx-web-2.0.0.web

Right now I'm using the following plugins to build: maven-compiler-plugin and maven-release-plugin

现在我正在使用以下插件来构建:maven-compiler-plugin 和 maven-release-plugin

What would be the best way to achieve the results I expect?

实现我期望的结果的最佳方法是什么?

采纳答案by Pascal Thivent

In your ear module, you can either use the project.build.finalNameelement or you can configure the maven-ear-pluginwhich supports a finalNameoptional parameter. And to configure the final name of a bundled jar, you'll need to define a jarModuleand to set the bundleFileNameproperty for it.

在您的 ear 模块中,您可以使用该project.build.finalName元素,也可以配置支持可选参数的maven-ear-pluginfinalName。要配置捆绑 jar 的最终名称,您需要定义 ajarModule并为其设置bundleFileName属性。

The final configuration might looks something like that (I'll demonstrate how to set the the final ear name in the plugin configuration here):

最终的配置可能看起来像这样(我将在这里演示如何在插件配置中设置最终的耳朵名称):

<project>
  [...]
  <dependencies>
    <dependency>
      <groupId>yourgroupid</groupId>
      <artifactId>projectx-client</artifactId>
      <version>2.0.0</version><!-- not mandatory if inherited -->
    </dependency>
    [...]
  </dependencies>
  [...]  
  <build>
    [...]
    <plugins>
      [...]
      <plugin>
        <artifactId>maven-ear-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <finalName>projectx-ear</finalName>
          <modules>
            <jarModule>
              <groupId>yourgroupid</groupId>
              <artifactId>projectx-client</artifactId>
              <bundleFileName>anotherName.jar</bundleFileName>
            </jarModule>
          </modules>
        </configuration>
      </plugin>
      [...]
</project>

回答by Péter T?r?k

You can achieve this by specifying the finalNameproperty in your pom like this:

您可以通过finalName在 pom 中指定属性来实现这一点:

<build>
    <finalName>${project.artifactId}</finalName>
</build>

回答by Daniele Licitra

Point one : single JAR/WAR.

第一点:单个 JAR/WAR。

To create it without version number in name, in pom.xml simply add the code:

要在名称中创建没有版本号的文件,只需在 pom.xml 中添加以下代码:

<build>
  <finalName>${project.artifactId}</finalName>
</build>

So, if you compile, for example projectx_misc, you get projectx_misc.jar.

所以,如果你编译,例如 projectx_misc,你会得到projectx_misc.jar

What happens to me? When I compile the same package from a POM project (in Netbeans), all the jars have the version in the name and the EAR include the xxx-version.jar archives.

我怎么了?当我从 POM 项目(在 Netbeans 中)编译相同的包时,所有 jar 的名称都包含版本,并且 EAR 包含 xxx-version.jar 档案。

So, point two: the EAR problem.

所以,第二点:EAR 问题。

In the EAR's pom.xml file, change the maven-ear-pluginadding in the configuration this property:

在 EAR 的 pom.xml 文件中,更改maven-ear-plugin在配置中添加此属性:

<fileNameMapping>no-version</fileNameMapping>

This is my plugin setting for example:

这是我的插件设置,例如:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-ear-plugin</artifactId>
   <version>2.8</version>
   <configuration>
       <version>6</version>
       <defaultLibBundleDir>lib</defaultLibBundleDir>
       <fileNameMapping>no-version</fileNameMapping>
   </configuration>
</plugin>

So, now when i compile all the archives from a project pom, all the version numbers are removed from the jars.

所以,现在当我从项目 pom 编译所有档案时,所有版本号都从 jars 中删除。