代码之家  ›  专栏  ›  技术社区  ›  coderatchet

在maven中创建并安装de lomboked源jar

  •  0
  • coderatchet  · 技术社区  · 7 年前

    免责声明:我已经解决了这个问题,并记录的解决方案,让世界知道。

    如何创建和安装 *-sources.jar 在maven中包含“delomboked”源代码?

    默认情况下,maven源插件会创建一个源jar,而不删除源文件,这会导致依赖库二进制文件的项目抱怨源文件不匹配。

    1 回复  |  直到 7 年前
        1
  •  8
  •   coderatchet    7 年前

    TL;DR(解释如下)

    plugins 中的配置 project.build

    <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 将使您能够删除源代码( ${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源插件,因为您不能在后面的中指定自定义源目录。jar任务指向我们自定义的“生成的源”,并生成名为sources jar的标准源。

    maven-install-plugin install-file 使用goal是因为不能使用 install 目标。我们可以通过使用 安装文件 sources .

    我希望这有助于其他人谁是斗争的街道像我是这个问题。

        2
  •  4
  •   xap    5 年前

    <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>
    
        3
  •  0
  •   Alexander Biryukov    5 年前

    有一种简单(但要复杂一点)的方法来实现你想要的功能:让你自己的 Maven Plugin .

    听起来太复杂了,但我们可以重复使用 maven-sources-plugin

    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());
        }
    
    }
    

    available here .

    推荐文章