Class 在maven程序集之前替换类文件

Class 在maven程序集之前替换类文件,class,maven,jar,resources,package,Class,Maven,Jar,Resources,Package,我面临着一个非常独特的情况,需要一些建议: 我正在做一个依赖于同一家公司的其他项目的项目,现在我需要对依赖项进行一些修改。 我遇到的问题是,依赖项的源代码已经丢失,因此我唯一拥有的就是存储库中的maven依赖项,以及相应的jar文件。 除此之外,Jar文件中的一些类是使用JiBX解析器创建的,映射了一些我没有的XSD文件,生成的类是合成的,我发现没有反编译器能够正确处理它们 所有这一切的唯一好处是,我需要更改的类可以正确地反编译,因此选择了以下内容: <build> <plug

我面临着一个非常独特的情况,需要一些建议: 我正在做一个依赖于同一家公司的其他项目的项目,现在我需要对依赖项进行一些修改。 我遇到的问题是,依赖项的源代码已经丢失,因此我唯一拥有的就是存储库中的maven依赖项,以及相应的jar文件。 除此之外,Jar文件中的一些类是使用JiBX解析器创建的,映射了一些我没有的XSD文件,生成的类是合成的,我发现没有反编译器能够正确处理它们

所有这一切的唯一好处是,我需要更改的类可以正确地反编译,因此选择了以下内容:

<build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.0</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <artifactSet>
                        <includes>
                            <include>${project.groupId}:${project.artifactId}</include>
                            <include>TheDamnedProject:WithoutSources</include>
                        </includes>
                    </artifactSet>
                    <filters>
                        <filter>
                            <artifact>TheDamnedProject:WithoutSources</artifact>
                            <includes>
                                <!-- These classes will be taken directly from dependency JAR -->
                                <include>my/package/ClassWhichCouldNotBeDecompiled1.class</include>
                                <include>my/package/ClassWhichCouldNotBeDecompiled2.class</include>
                                <include>my/package/ClassWhichCouldNotBeDecompiled3.class</include>
                                <include>my/package/ClassWhichCouldNotBeDecompiled4.class</include>
                            </includes>
                        </filter>
                        <filter>
                            <artifact>${project.groupId}:${project.artifactId}</artifact>
                            <excludes>
                                <!-- These classes will be overridden by the ones inside the JAR -->
                                <exclude>my/package/ClassWhichCouldNotBeDecompiled1.class</exclude>
                                <exclude>my/package/ClassWhichCouldNotBeDecompiled2.class</exclude>
                                <exclude>my/package/ClassWhichCouldNotBeDecompiled3.class</exclude>
                                <exclude>my/package/ClassWhichCouldNotBeDecompiled4.class</exclude>
                            </excludes>
                        </filter>
                    </filters>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>
  • 我反编译了整个jar文件,最后得到了一些类( JiBx)中包含空的或错误实现的方法
  • 我注释掉了使用存根对象的错误方法的主体,将所需的更改应用到正确的类并重新编译
  • 我获取了旧的Jar文件,打开它,并手动将旧类替换为新类
由此产生的Jar按预期工作

现在我的问题是:我能用Maven做所有这些吗

其想法是将JiBX类文件作为资源,并将存根等价物作为源文件,然后让maven:

  • 像往常一样编译所有内容,将所有已编译的类文件 进入目标文件夹
  • 从目标文件夹中删除存根类文件,并用旧的预编译类文件替换它们
  • 把罐子打包
你会推荐哪种方法

更新

我将提供有关依赖项项目结构的更多详细信息:

所有类都在同一个包中:

my.project.domain.JiBX__c_GeneratedObfuscatedClass1.java
my.project.domain.JiBX__c_GeneratedObfuscatedClass2.java
my.project.domain.JiBX__c_GeneratedObfuscatedClass3.java
my.project.domain.CustomizableClass1.java
my.project.domain.CustomizableClass2.java
my.project.domain.CustomizableClass3.java
JiBX类没有作为依赖项正确导入,如果我尝试将任何CustmizableClasses放入项目源代码中,并让JiBX类处于依赖项中,编译器会报告缺少的方法

我还尝试使用建议的Shade插件,但由于我需要将JiBX类包含到源路径中,因此我最终不得不将jar依赖项和编译的自定义类包含到包JiBX类中,但跳过jar dep和编译的JiBX类中的自定义类

我看起来可以,但我承认我还是没有找到方法。 任何线索都将受到欢迎

更新2(分辨率)

我在这里解释了我最终是如何按照建议使用shade插件实现这一点的,以防其他人也需要这样做:

我最终在同一个包中创建了一个包含反编译类的项目,并将不希望反编译的方法注释掉

在pom.xml中,我添加了以下内容:

<build>
<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.0</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <artifactSet>
                        <includes>
                            <include>${project.groupId}:${project.artifactId}</include>
                            <include>TheDamnedProject:WithoutSources</include>
                        </includes>
                    </artifactSet>
                    <filters>
                        <filter>
                            <artifact>TheDamnedProject:WithoutSources</artifact>
                            <includes>
                                <!-- These classes will be taken directly from dependency JAR -->
                                <include>my/package/ClassWhichCouldNotBeDecompiled1.class</include>
                                <include>my/package/ClassWhichCouldNotBeDecompiled2.class</include>
                                <include>my/package/ClassWhichCouldNotBeDecompiled3.class</include>
                                <include>my/package/ClassWhichCouldNotBeDecompiled4.class</include>
                            </includes>
                        </filter>
                        <filter>
                            <artifact>${project.groupId}:${project.artifactId}</artifact>
                            <excludes>
                                <!-- These classes will be overridden by the ones inside the JAR -->
                                <exclude>my/package/ClassWhichCouldNotBeDecompiled1.class</exclude>
                                <exclude>my/package/ClassWhichCouldNotBeDecompiled2.class</exclude>
                                <exclude>my/package/ClassWhichCouldNotBeDecompiled3.class</exclude>
                                <exclude>my/package/ClassWhichCouldNotBeDecompiled4.class</exclude>
                            </excludes>
                        </filter>
                    </filters>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

org.apache.maven.plugins
maven阴影插件
2
包裹
阴凉处
${project.groupId}:${project.artifactId}
DamnedProject:无外包
DamnedProject:无外包
我的/包/类无法编译1.class
我的/包/类无法编译2.class
我的/包/类无法编译3.class
我的/包/类无法编译4.class
${project.groupId}:${project.artifactId}
我的/包/类无法编译1.class
我的/包/类无法编译2.class
我的/包/类无法编译3.class
我的/包/类无法编译4.class

谢谢


Carles

以下是我将如何做到这一点:

  • 为此Jar文件创建一个新的Maven项目,打包类型为Jar
  • 将原始Jar文件作为依赖项包括在内
  • 在src文件夹中添加一个反编译的.java文件
这将使您能够编译.java文件,因为jar文件中的其他类都是可用的

现在您有两个Jar文件:一个是原始的,另一个应该只包含一个重新编译和更改的类

将两者添加到应用程序类路径可能会起作用,但这取决于类路径上的顺序

如果您希望得到一个jar文件,我建议您看看Maven Shade Plugin(),它将允许您创建一个包含多个源内容的新jar文件。它将允许您过滤进入新Jar文件的内容

Maven Shade插件允许您指定包含每个工件的类。它为此使用通配符和包含/排除标记,如下所述:

一旦创建了Jar文件,我将使用Maven发布插件发布它,然后在下游包含该工件。这将允许您仅在确实需要时更新修补过的Jar,可能不必在每个构建中都进行更新。但这取决于你的使用模式

对于新Jar文件的版本号,我建议使用原始Jar文件的变体。使用相同的
groupId
artifactId
,然后修改