Java Maven“阴影”JAR 在文件名中以“原始”为前缀

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

Maven "shaded" JAR is prefixed with "original" in the file name

javamaven-2

提问by Steve Perkins

I'm using the "shade" Maven2 plugin to build a monolithic JAR with all Java dependencies bundled together. The relevant section in pom.xmlis pretty straightforward:

我正在使用“shade”Maven2 插件来构建一个整体 JAR,其中所有 Java 依赖项都捆绑在一起。中的相关部分pom.xml非常简单:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>1.4</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <finalName>${project.artifactId}-${project.version}-SHADED</finalName>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.mypackage.MyClass</mainClass>
                    </transformer>
                </transformers>
            </configuration>
        </execution>
    </executions>
</plugin>

However, the build results are odd. It seems that TWO files are actually created by this Maven plugin:

但是,构建结果很奇怪。这个Maven插件似乎实际上创建了两个文件:

myartifact-1.0.0-SHADED.jar  (zero bytes)
original-myartifact-1.0.0-SHADED.jar  (10 MB)

The JAR file with prefix "original" is properly built, and functions just fine. I suppose I could just rename it to strip off that prefix, and go on my merry way.

带有前缀“original”的 JAR 文件已正确构建,并且功能正常。我想我可以重命名它以去除该前缀,然后继续我的快乐之路。

However, I very curious as to what may be going on here with the "shade" plugin. It looks like the "original" file is temporary working space type thing, intended to be renamed at the end of the process, and that final renaming simply doesn't complete. There's no obvious explanation for that, though (i.e. filesystem permissions, etc). Has anyone ever seen this before?

但是,我很好奇“阴影”插件可能会在这里发生什么。看起来“原始”文件是临时工作空间类型的东西,打算在过程结束时重命名,而最终的重命名根本没有完成。不过,对此没有明显的解释(即文件系统权限等)。有没有人见过这个?

采纳答案by Anon

Maven's build steps will create the jar target/artifact-version.jar.

Maven 的构建步骤将创建 jar target/artifact-version.jar

Then the shade plugin runs. It normally renames that jar to target/original-artifact-version.jar, and gives the shaded JAR the name target/artifact-version.jar.

然后阴影插件运行。它通常将该 jar 重命名为target/original-artifact-version.jar,并为着色的 JAR 命名target/artifact-version.jar

However, you're configuring the Shade plugin to use a different name. Unless there's a good reason for that, I'd remove <finalName>from your configuration, and live with what the Shade plugin wants to give you.

但是,您正在配置 Shade 插件以使用不同的名称。除非有充分的理由,否则我会<finalName>从你的配置中删除,并接受 Shade 插件想要给你的东西。

回答by Stewart

I know this question is old, but thought it was worth adding the following information.

我知道这个问题很老,但认为值得添加以下信息。

I think the output originally desired by Steve is that given on this page of the maven-shade-plugin documentation.

我认为史蒂夫最初想要的输出是在maven-shade-plugin 文档的这个页面上给出的。

<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>jar-with-dependencies</shadedClassifierName>

回答by Roberto Andrade

Following @Stewart on providing a slightly better answer (no offense to either :D):

跟随@Stewart 提供一个稍微好一点的答案(没有冒犯任何一个:D):

The reason you get the original-* mess can be twofold:

您得到原始-* 混乱的原因可能是双重的:

  1. Specifying <finalName>means you want a name different than what Maven gives you by default (ie: same as artifact's name: artifactId-version.jar or artifactId-version-shaded.jar). If you specify a final name that is the same as one of the two, it will try to back the old one up as original-*.jar and then overwrite it with the new shaded one. In your case, you're telling it to make the final JAR *-shaded.jar, which is already the case when it comes out of Maven (before they by default make it into artifactId-version.jar), so it first backs up the old *-shaded.jaras original-*-shaded.jarand then bugs out when writing the bytes to the new *-shaded.jargiven the old one disappeared (they seem to rename it).

  2. (Which was my case) Using <shadedClassifierName>, which changes only the suffix Maven uses to generate the *-shaded.jar, in combination with <finalName>can also yield the same results. If you want you can just use <shadedClassifierName>and specify a different suffix and be done with it without having to specify <finalName>with the whole thing. In my case I had both set up to name the output the same thing: ie: artifactId-version-all.jar but using 'all' as Classifier brought me back to the scenario described in #1.

  1. 指定<finalName>意味着你想要一个不同于 Maven 给你的默认名称(即:与工件的名称相同:artifactId-version.jar 或 artifactId-version-shaded.jar)。如果您指定的最终名称与两者之一相同,它将尝试将旧名称备份为 original-*.jar,然后用新的阴影名称覆盖它。在你的情况下,你告诉它制作最终的 JAR *-shaded.jar,当它从 Maven 出来时已经是这种情况(在它们默认进入 artifactId-version.jar 之前),所以它首先支持了旧*-shaded.jaroriginal-*-shaded.jar,然后虫子写字节到新外出时*-shaded.jar给旧的消失了(他们似乎将其重命名)。

  2. (这就是我的情况) Using <shadedClassifierName>,它仅更改 Maven 用于生成 *-shaded.jar 的后缀,结合 with<finalName>也可以产生相同的结果。如果您愿意,您可以使用<shadedClassifierName>并指定不同的后缀并完成它,而无需指定<finalName>整个内容。在我的情况下,我都设置为输出命名相同的东西:即: artifactId-version-all.jar 但使用“all”作为分类器让我回到#1 中描述的场景。