Java 避免 maven-jar 的最佳方法是什么?

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

What is the best way to avoid maven-jar?

javamaven-2antjarmaven-plugin

提问by unj2

I am using a different plugin (ant4eclipse) to jar my files. What is the best way to avoid the maven-jar plugin from executing?

我正在使用不同的插件 (ant4eclipse) 来 jar 我的文件。避免执行 maven-jar 插件的最佳方法是什么?

  • I tried to remove the <plugin>maven-jar-plugin</plugin>
  • I tried to <exclude> ** / * < / exclude>
  • I tried to <skip>true</skip>
  • 我试图删除 <plugin>maven-jar-plugin</plugin>
  • 我试过了 <exclude> ** / * < / exclude>
  • 我试过了 <skip>true</skip>

None worked

没有工作

回答by Drew Wills

What happens if you declare this?

如果你声明这一点会发生什么?

<packaging>pom</packaging>

Even if it does what you're looking for, be careful. I'm not sure if there could be negative side effects -- such as other maven projects that depend on your jar not being able to locate it.

即使它符合您的要求,也要小心。我不确定是否会有负面影响——比如其他依赖于你的 jar 的 maven 项目无法找到它。

回答by Pascal Thivent

I am using a different plugin to jar my files. What is the best way to avoid the maven-jar plugin from executing?

我正在使用不同的插件来 jar 我的文件。避免执行 maven-jar 插件的最佳方法是什么?

First, the jar:jargoal is bound by defaulton the packagephase for a project with a packaging of type jar. Second, there is no way to unbind a plugin bound to a phase. So, if you are using another plugin(?), either accept to produce 2 JARs or change the packaging (but I don't think this will work well).

首先,jar:jar目标在默认情况下绑定package具有类型打包的项目的阶段上jar。其次,无法解除绑定到阶段的插件。因此,如果您正在使用另一个插件(?),要么接受生成 2 个 JAR 或更改包装(但我认为这不会很好)。

回答by bmargulies

Explicitly bind the jar plugin to a phase that doesn't exist.

将 jar 插件显式绑定到不存在的阶段。

回答by Brett Porter

As other's have said, it's not possible to turn it off, other than using <packaging>pom</packaging>, which turns everything off and is probably not what you want.

正如其他人所说,除了使用 之外<packaging>pom</packaging>,不可能将其关闭,这会关闭所有内容并且可能不是您想要的。

Even though it will generate twice, a working solution is to bind your jar process to the packagephase, as that is guaranteed to run after the default. By overwriting the same JAR file, you'll find that yours is used wherever the original would have been.

即使它会生成两次,一个可行的解决方案是将您的 jar 进程绑定到package阶段,因为它保证在默认值之后运行。通过覆盖相同的 JAR 文件,您会发现在原始文件所在的任何地方都可以使用您的文件。

回答by peterj

In Maven 3.0.x (I tried 3.0.2) you can disable maven-jar-pluginby binding the default-jarexecution to a nonexistent phase, as @bmargulies suggested. Unfortunately that doesn't work in 2.2.1, but you can prevent it from interfering with your own jar by setting an alternative <finalName>and <classifier>for the default-jarexecution; it will still create a jar, but it will be set as a secondary artifact for the project and won't overwrite the one you've created. Here's an example that should work in both Maven 2 and Maven 3:

在 Maven 3.0.x(我尝试过 3.0.2)中,您可以maven-jar-plugin通过将default-jar执行绑定到不存在的阶段来禁用,正如@bmargulies 建议的那样。不幸的是不起作用的2.2.1,但你可以通过设置一个替代防止其与自己的罐子干扰<finalName><classifier>default-jar执行; 它仍然会创建一个 jar,但它将被设置为项目的辅助工件,并且不会覆盖您创建的那个。这是一个适用于 Maven 2 和 Maven 3 的示例:

<project>
  <modelVersion>4.0.0</modelVersion>

  <groupId>test</groupId>
  <artifactId>test</artifactId>
  <version>0.1-SNAPSHOT</version>

  <build>
    <plugins>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <executions>
          <execution>
            <id>default-jar</id>
            <phase>none</phase>
            <configuration>
              <finalName>unwanted</finalName>
              <classifier>unwanted</classifier>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Once you've disabled maven-jar-plugin, maven-install-pluginmay give you trouble too. In Maven 3 it can be disabled the same as maven-jar-plugin: bind default-installto a nonexistent phase. However, in Maven 2 maven-install-pluginrequires that the target/classesdirectory exist, and it will install the dummy jar when there isn't a primary artifact present.

一旦你禁用了maven-jar-pluginmaven-install-plugin也可能会给你带来麻烦。在 Maven 3 中,它可以像maven-jar-plugin: binddefault-install到不存在的阶段一样被禁用。但是,在 Maven 2 中maven-install-plugin要求该target/classes目录存在,并且当不存在主要工件时,它将安装虚拟 jar。

回答by Chris

       <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <skip>true</skip>
            </configuration>
        </plugin>        

回答by redhot

This should do the trick - notice the use of <id>default-jar</id>and <phase/>.

这应该做的伎俩-请注意使用的<id>default-jar</id><phase/>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>default-jar</id>
            <phase/>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

回答by bheussler

In my case, I only wanted to disable the jar plugin because the jar was empty. You can use the skipIfEmptyoption in the plugin configuration

就我而言,我只想禁用 jar 插件,因为 jar 是空的。您可以使用skipIfEmpty插件配置中的选项

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.0.2</version>
    <configuration>
        <skipIfEmpty>true</skipIfEmpty>
    </configuration>
</plugin>

回答by Erik Nellessen

Using maven 3.3.9, the following worked for me:

使用 maven 3.3.9,以下对我有用:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.1.2</version>
    <executions>
        <execution>
            <id>default-jar</id>
            <phase>none</phase>
            <configuration>
                <finalName>unwanted</finalName>
                <classifier>unwanted</classifier>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>3.0.0-M1</version>
    <configuration>
        <skip>true</skip>
    </configuration>
</plugin>

So in case of the maven-jar-plugin, I bound it to a non-existent phase. For the maven-install-plugin, I used the "skip" configuration parameter. The documentationabout it says: "Set this to true to bypass artifact installation. Use this for artifacts that does not need to be installed in the local repository."

所以在 maven-jar-plugin 的情况下,我将它绑定到一个不存在的阶段。对于 maven-install-plugin,我使用了“skip”配置参数。关于它的文档说:“将此设置为 true 以绕过工件安装。将其用于不需要安装在本地存储库中的工件。”