在Maven中创建并安装取消登录的源jar

qcuzuvrc  于 2022-11-22  发布在  Maven
关注(0)|答案(4)|浏览(150)

免责声明:我已经解决了这个问题,并正在记录解决方案,让世界知道。
如何在maven中创建并安装包含“delomboked”源代码的***-sources.jar**?
默认情况下,maven-source-plugin会创建一个sources jar,而不会取消源文件的目录,这会导致依赖于库二进制文件的项目抱怨源文件不匹配。

fnx2tebb

fnx2tebb1#

TL;DR(解释如下)

将以下插件配置添加到pom.xml的project.build元素中的plugins配置中

<project>
    ...
<build>
<plugins>
    ...
<plugin>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok-maven-plugin</artifactId>
    <version>1.18.0.0</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <goals>
                <goal>delombok</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <sourceDirectory>src/main/java</sourceDirectory>
        <outputDirectory>${project.build.directory}/delombok</outputDirectory>
        <addOutputDirectory>false</addOutputDirectory>
        <encoding>UTF-8</encoding>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <id>copy-to-lombok-build</id>
            <phase>process-resources</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <resources>
                    <resource>
                        <directory>${project.basedir}/src/main/resources</directory>
                    </resource>
                </resources>
                <outputDirectory>${project.build.directory}/delombok</outputDirectory>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>generate-delomboked-sources-jar</id>
            <phase>package</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target>
                    <jar destfile="${project.build.directory}/${project.build.finalName}-sources.jar"
                         basedir="${project.build.directory}/delombok"/>
                </target>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.5.2</version>
    <executions>
        <execution>
            <id>install-source-jar</id>
            <goals>
                <goal>install-file</goal>
            </goals>
            <phase>install</phase>
            <configuration>
                <file>${project.build.directory}/${project.build.finalName}-sources.jar</file>
                <groupId>${project.groupId}</groupId>
                <artifactId>${project.artifactId}</artifactId>
                <version>${project.version}</version>
                <classifier>sources</classifier>
                <generatePom>true</generatePom>
                <pomFile>${project.basedir}/pom.xml</pomFile>
            </configuration>
        </execution>
    </executions>
</plugin>
</plugins>
</build>
</project>

说明

**lombok-maven-plugin**将使您能够使用delombok源代码(${project.basedir}/src/main/java)并将其放置在目标目录中(${project.build.directory}/delombok)。通常这会将代码放在${project.build.directory}/generated-sources/delombok文件夹中,但由于Intellij会自动考虑此附加源代码,因此在开发库时会出现重复代码错误,为了阻止这种情况,只需指定一个非默认的目标目录(在本例中,就在generated-sources目录之外)。
**maven-resources-plugin**是必要的,以便从标准${project.basedir}/src/main/resources目录复制资源。如果您的项目中有任何其他非标准资源目录,您应该在此插件的资源部分配置它们。
**maven-antrun-plugin**代替maven-source-plugin使用,因为您不能在后者中指定自定义源目录。jar任务指向我们的自定义“generated-sources”并生成标准命名的源jar。
**maven-install-plugin**使用install-file目标是因为您无法使用install目标附加jar。我们可以通过使用install-file目标和sources分类器手动安装文件来破解解决方案。

我希望这能帮助其他像我一样在奋斗街上的人解决这个问题。

j2cgzkjk

j2cgzkjk2#

下面的解决方案是基于上面提供的解决方案,但是通过使用build-helper插件来附加生成的已删除目录的源jar而不是使用install-file来改进它。这样做的好处是,正常的maven安装和部署阶段可以正确处理生成的文件,就像使用源插件一样。

<project>
...
<build>
    <plugins>
        ...
        <plugin>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok-maven-plugin</artifactId>
            <version>1.18.12.0</version>
            <executions>
                <execution>
                    <id>delombok-sources</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>delombok</goal>
                    </goals>
                    <configuration>
                        <sourceDirectory>src/main/java</sourceDirectory>
                        <outputDirectory>${project.build.directory}/delombok</outputDirectory>
                        <addOutputDirectory>false</addOutputDirectory>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <id>generate-delomboked-sources-jar</id>
                    <phase>package</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                    <configuration>
                        <target>
                            <jar destfile="${project.build.directory}/${project.build.finalName}-sources.jar"
                                 basedir="${project.build.directory}/delombok"/>
                        </target>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <id>attach-delomboked-sources-jar</id>
                    <phase>package</phase>
                    <goals>
                        <goal>attach-artifact</goal>
                    </goals>
                    <configuration>
                        <artifacts>
                            <artifact>
                                <file>${project.build.directory}/${project.build.finalName}-sources.jar</file>
                                <type>jar</type>
                                <classifier>sources</classifier>
                            </artifact>
                        </artifacts>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
</project>
yv5phkfx

yv5phkfx3#

需要指出的是,也可以使用概要文件(以避免构建源目录不可自定义)。
将以下内容添加到pom.xml属性中:

<origSourceDir>${project.basedir}/src/main/java</origSourceDir>
<sourceDir>${origSourceDir}</sourceDir>
<delombokedSourceDir>${project.build.directory}/delombok</delombokedSourceDir>
</properties>

配置文件和构建部分更改:

<profiles>
    <profile>
        <id>build</id>
        <properties>
            <sourceDir>${delombokedSourceDir}</sourceDir>
        </properties>
    </profile>
</profiles>
<build>
    <sourceDirectory>${sourceDir}</sourceDirectory>
    <plugins>
        <plugin>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok-maven-plugin</artifactId>
            <version>1.18.20.0</version>
            <executions>
                <execution>
                    <id>delombok</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>delombok</goal>
                    </goals>
                    <configuration>
                        <addOutputDirectory>false</addOutputDirectory>
                        <sourceDirectory>${origSourceDir}</sourceDirectory>
                        <outputDirectory>${delombokedSourceDir}</outputDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <version>3.0.1</version>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

使用mvn clean install -Pbuild执行
这应该可以解决IntelliJ中的“Library source does not match the bytecode for class”(库源代码与类的字节码不匹配)错误,并在大多数情况下允许无缝调试。
参考:“Delombok plugin + profile in maven

pu82cl6c

pu82cl6c4#

对于多模块项目或纯pom项目,这两种方法都有缺陷,因为没有源代码,所以必须创建一个空目录,它将生成一个空的. jar。
有一种简单(但稍微复杂一点)的方法可以实现所需的功能:做你自己的Maven Plugin
听起来过于复杂,但我们可以重用maven-sources-plugin并用MOJO的继承更新必要的部分:

import java.util.List;
import java.util.stream.Collectors;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.source.SourceJarNoForkMojo;
import org.apache.maven.project.MavenProject;

/**
 * This goal bundles all the sources into a jar archive, but uses delomboked sources.
 */
@Mojo(name = "jar-no-fork", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true)
public class EnhancedSourceJarNoForkMojo extends SourceJarNoForkMojo {

    @Parameter(property = "<some-prefix>.useDelombokSources", defaultValue = "true")
    protected boolean useDelombokSources;

    @Parameter(property = "<some-prefix>.delombokSourcesLocation", defaultValue = "delombok")
    protected String delombokSourcesLocation;

    @Override
    protected List<String> getSources(MavenProject p) {
        // if user doesn't want delomboked sources, use default algorithm
        List<String> sources = super.getSources(p);
        if (!useDelombokSources) {
            return sources;
        }

        // typically, sources' list will contain: [src/main/java, target/generated_sources].
        // replace src/main/java if it's present with delombok-generated sources
        String target = p.getBuild().getDirectory();
        return super.getSources(p)
                .stream()
                .map(s -> s.endsWith("java") ? String.format("%s/%s", target, delombokSourcesLocation) : s)
                .collect(Collectors.toList());
    }

}

pom.xml例程的要点是available here

相关问题