Maven Shade插件会在Windows上自动将包名称转换为小写吗?

时间:2018-07-05 05:47:04

标签: java linux windows maven case-sensitive

我有两个本地工件:一个带有com.org.abc,另一个带有COM.org.xyz。我创建了一个包含这两个以及所有其他所需依赖项的阴影罐。

当我在LINUX上创建带阴影的JAR时,会创建两个单独的文件夹:com和COM。 WINDOWS上仅创建一个文件夹。

当我在Windows上创建一个有阴影的jar时,它将创建一个文件夹:com.org,其中包含abc和xyz文件夹。不会创建单独的大写COM文件夹。因此,依赖于大写 COM 程序包的代码失败,无法初始化类错误。

(我没有命名上述2个,它们是由2个独立的团队分别创建和分发的,而且许多团队一直在使用这些jar,因此更改程序包名称的过程很长)

Maven配置:

<plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.5.2</version>
                <executions>
                    <execution>
                        <id>add-mylocal</id>
                        <phase>clean</phase>
                        <configuration>
                            <file>${jars.path}/mylocal.jar</file>
                            <repositoryLayout>default</repositoryLayout>
                            <groupId>com.org</groupId>
                            <artifactId>mylocal</artifactId>
                            <version>1.0</version>
                            <packaging>jar</packaging>
                            <generatePom>true</generatePom>
                            <localRepositoryPath>${local.repo.path}</localRepositoryPath> 
                        </configuration>
                        <goals>
                            <goal>install-file</goal>
                        </goals>
                    </execution>    
                </executions>
            </plugin>
            <plugin>                                
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>                 
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <source>1.7</source>
                            <target>1.7</target>
                            <transformers>
                                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                  <manifestEntries>
                                    <Build-Version>${buildversion} (${timestamp})</Build-Version>
                                  </manifestEntries>
                                </transformer>
                            </transformers>
                        </configuration>                                                                        
                    </execution>                                        
                </executions>
            </plugin>
        </plugins>

有什么解决方案可以在Windows上运行吗?

3 个答案:

答案 0 :(得分:2)

记录评论中的讨论内容以作为后代的答案:

这里的问题不是maven-shade-plugin,它不会修改(甚至不在乎)程序包的大小写。这里的问题是基础[windows]文件系统不区分大小写,并且不能区分comCOM

除非您愿意更改软件包名称,否则maven-shade-plugin内没有解决方法。您必须使用区分大小写的文件系统。一种有吸引力的选择是使用Windows Subsystem for Linux,它提供了自己的区分大小写的文件系统(ext4,IIRC)。

还有一种使NTFS文件系统区分大小写的方法(例如,this SU thread),但我本人从未这样做过,因此不能根据个人经验推荐它。

答案 1 :(得分:1)

虽然maven阴影实际上似乎删除了大写的COM目录,但实际上将其与小写的目录合并。

这个区别似乎很小,但是允许我们使用Maven阴影的特殊部分来解决此问题,即重定位功能。

使用此功能,我们可以将奇怪的命名为大写的库重新定位为小写,而无需更改这些库的源。

<relocations>
    <relocation>
        <pattern>COM.org.xyz</pattern>
        <shadedPattern>com.org.xyz</shadedPattern>
    </relocation>
</relocations>

您最终的阴影配置将如下所示:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <transformers>
                    <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                      <manifestEntries>
                        <Build-Version>${buildversion} (${timestamp})</Build-Version>
                      </manifestEntries>
                    </transformer>
                </transformers>
                <relocations>
                    <relocation>
                        <pattern>ME.ferrybig.uppercase.com</pattern>
                        <shadedPattern>me.ferrybig.uppercase.com</shadedPattern>
                    </relocation>
                </relocations>
            </configuration>
        </execution>
    </executions>
</plugin>

答案 2 :(得分:0)

我找到了解决上述问题的方法。尽管Ferrybig和Mureinik在回答中也建议最佳做法是对项目实施小写的软件包名称标准,但是由于在我的情况下这是不可能的,因此我遵循以下方法。

简而言之:

在Windows上,shade插件将 COM 文件夹与 com 合并,因为Windows将它们区分大小写,因此,如果软件包 com 是已经创建的文件,只会在其中添加 COM 的内容,而不是创建新的文件。

解决方案:

在我的shade插件中,我创建了2个uber jars-一个包含大写COM包,另一个包含所有其他依赖项。之所以解决了问题,是因为在第一个jar中与 com 没有冲突,因为它只包含 COM

我使用的配置来自this post.

基本上在第一个执行块中,我包含了包含COM软件包的工件,并从第二个执行块中排除了这些工件:

执行块1:

<include><artifact_name_with_COM_package></include> 

执行块2:

<exclude><artifact_name_with_COM_package></exclude> 

注意:再次,首选应该是在软件包中强制使用命名标准。但是,如果您想快速解决问题,可以尝试一下。