Maven:库的开发和发布版本

发布于 2024-12-11 14:31:34 字数 362 浏览 0 评论 0原文

我有一个由 3 个库组成的项目 - 我们称它们为 1) BABY、2) CHILD 和 3) ADULT。库“CHILD”取决于“BABY”,“ADULT”取决于“CHILD”。

我想要做的是生成:

  • 具有所有(传递)依赖项的开发版本
  • 为每个库创建独立 JAR 的生产版本(嵌入依赖项)

我有一个配置文件 dev 和一个配置文件已经发布,并且我知道如何使用 ProGuard 生成 JAR。

问题是如何告诉 Maven 将所有依赖项保留在 dev 中,并在 生产 中忽略它们(可选/提供)?

I have a project consisting of 3 libraries - let's call them 1) BABY, 2) CHILD and 3) ADULT. Lib "CHILD" depends on "BABY" and "ADULT" depends on "CHILD".

What I want to do is produce:

  • a dev version that has all the (transitive) dependencies
  • a production version that creates a standalone JAR for each library (embedding the dependencies)

I have a profile dev and a profile release already, and I know how to use ProGuard to generate the JAR.

The question is how to tell Maven to keep all dependencies in dev and ignore them (optional/provided) in production?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷…雨湿花 2024-12-18 14:31:34

要在开发部署时拥有不同的依赖项,可以使用 Maven 配置文件。

http://maven.apache.org/guides/introduction/introduction- to-profiles.html

因此,在开发时,您会使用类似 mvn -Pdevcompile

当您说“独立 jar”时,听起来您的意思是一个将所有依赖项合并到其中的 jar。

如何使用以下命令创建具有依赖项的可执行 JAR Maven?

http://maven.apache.org/plugins/maven-assemble-plugin/

To have different dependencies when you develop to deployment you could use maven profiles.

http://maven.apache.org/guides/introduction/introduction-to-profiles.html

So when developing you would use something like mvn -Pdev compile

When you say "standalone jar" it sounds like you mean a jar with all dependencies merged into it.

How can I create an executable JAR with dependencies using Maven?

or http://maven.apache.org/plugins/maven-assembly-plugin/

背叛残局 2024-12-18 14:31:34

这是我最终使用的:

  • 父 POM 定义了一个配置文件 release,它配置 proguard 插件(打包一个大 JAR)和安装插件< /code> (将发布工件放入存储库中)。

  • lib-baby POM 仅调用 部分中的 2 个插件。

  • lib-child POM 还指定了一个 dev 配置文件,其中定义了对 lib-baby 的依赖关系。在发布配置文件中,此依赖项有一个 可选 标记,并包含在大 JAR 中。

最后,默认运行时,会创建库 com.company.dev:lib-babycom.company.dev:lib-child (包括它们的依赖项) 。

当使用 -Prelease 运行时,会创建库 com.company:lib-babycom.company:lib-child (独立库 [WITHOUT任何依赖项]) - 唯一的副作用是默认工件(.*dev)被覆盖:(

parent:

<project>
    <groupId>com.company</groupId>
    <artifactId>lib-parent</artifactId>
    <packaging>pom</packaging>

    <modules>
        <module>lib-baby</module>
        <module>lib-child</module>
        <module>lib-adult</module>
    </modules>

    <profiles>
        <profile>
            <id>release</id>
            <activation>
                <property>
                    <name>release</name>
                </property>
            </activation>

            <build>
                <pluginManagement>
                    <plugins>
                        <!-- aggregate to one big jar -->
                        <plugin>
                            <groupId>com.pyx4me</groupId>
                            <artifactId>proguard-maven-plugin</artifactId>
                            <executions>
                                ...
                            </executions>
                            <configuration>
                                <injar>${project.build.finalName}.jar</injar>
                                <outjar>${project.build.finalName}-release.jar</outjar>
                                ....
                            </configuration>
                        </plugin>

                        <!-- install release version of artifact separately (under com.company) -->
                        <plugin>
                            <inherited>true</inherited>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-install-plugin</artifactId>
                            <executions>
                                <execution>
                                    <id>install-release</id>
                                    <goals>
                                        <goal>install-file</goal>
                                    </goals>
                                    <configuration>
                                        <file>
                                            target/${project.build.finalName}-release.jar
                                        </file>
                                        <groupId>com.company</groupId>
                                        ...
                                    </configuration>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </pluginManagement>
            </build>
        </profile>
    </profiles>
</project>

lib-baby:

<project>
    <groupId>com.company.dev</groupId>
    <artifactId>lib-baby</artifactId>
    <packaging>jar</packaging>

    <parent>
        <groupId>com.company</groupId>
        <artifactId>lib-parent</artifactId>
    </parent>

    <profiles>    
        <profile>
            <id>release</id>

            <build>
                <plugins>
                    <!-- produces 'lib-baby-release.jar -->
                    <plugin>
                        <groupId>com.pyx4me</groupId>
                        <artifactId>proguard-maven-plugin</artifactId>
                    </plugin>

                    <!-- installs 'lib-baby-release.jar -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-install-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>install-release</id>
                                <phase>install</phase>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

lib-child:< /强>

<project>
    <groupId>com.company.dev</groupId>
    <artifactId>lib-child</artifactId>
    <packaging>jar</packaging>

    <parent>
        <groupId>com.company</groupId>
        <artifactId>lib-parent</artifactId>
    </parent>

    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>

            <dependencies>
                <dependency>
                    <groupId>com.company.dev</groupId>
                    <artifactId>lib-baby</artifactId>
                </dependency>
            </dependencies>
        </profile>

        <profile>
            <id>release</id>

            <dependencies>
                <dependency>
                    <groupId>com.company.dev</groupId>
                    <artifactId>lib-baby</artifactId>
                    <version>1.0</version>
                    <!-- made optional because will be embedded in standalone jar -->
                    <optional>true</optional>
                </dependency>
            </dependencies>

            <build>
                <plugins>
                    <!-- produces 'lib-child-release.jar -->
                    <plugin>
                        <groupId>com.pyx4me</groupId>
                        <artifactId>proguard-maven-plugin</artifactId>
                        <configuration>
                            <assembly>
                                <inclusions>
                                    <inclusion>
                                        <groupId>com.company.dev</groupId>
                                        <artifactId>lib-baby</artifactId>
                                    </inclusion>
                                </inclusions>
                            </assembly>
                        </configuration>
                    </plugin>

                    <!-- installs 'lib-child-release.jar -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-install-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>install-release</id>
                                <phase>install</phase>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

Here is what I used eventually:

  • The parent POM defines a profile release that configurates the proguard plugin (crates one big JAR) and the install plugin (places the release artifact in the repo).

  • The lib-baby POM simply calls the 2 plugins in the <build> section.

  • The lib-child POM additionally specifies a dev profile where the dependency to lib-baby is defined. Within the release profile this dependency has an optional tag and is included in the big JAR.

In the end when run by default, the libs com.company.dev:lib-baby and com.company.dev:lib-child are created (included their dependencies).

When run with -Prelease the libs com.company:lib-baby and com.company:lib-child are created (standalone libs [WITHOUT any dependencies]) - only side effect is that the default artifacts (.*dev) are overwritten :(

parent:

<project>
    <groupId>com.company</groupId>
    <artifactId>lib-parent</artifactId>
    <packaging>pom</packaging>

    <modules>
        <module>lib-baby</module>
        <module>lib-child</module>
        <module>lib-adult</module>
    </modules>

    <profiles>
        <profile>
            <id>release</id>
            <activation>
                <property>
                    <name>release</name>
                </property>
            </activation>

            <build>
                <pluginManagement>
                    <plugins>
                        <!-- aggregate to one big jar -->
                        <plugin>
                            <groupId>com.pyx4me</groupId>
                            <artifactId>proguard-maven-plugin</artifactId>
                            <executions>
                                ...
                            </executions>
                            <configuration>
                                <injar>${project.build.finalName}.jar</injar>
                                <outjar>${project.build.finalName}-release.jar</outjar>
                                ....
                            </configuration>
                        </plugin>

                        <!-- install release version of artifact separately (under com.company) -->
                        <plugin>
                            <inherited>true</inherited>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-install-plugin</artifactId>
                            <executions>
                                <execution>
                                    <id>install-release</id>
                                    <goals>
                                        <goal>install-file</goal>
                                    </goals>
                                    <configuration>
                                        <file>
                                            target/${project.build.finalName}-release.jar
                                        </file>
                                        <groupId>com.company</groupId>
                                        ...
                                    </configuration>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </pluginManagement>
            </build>
        </profile>
    </profiles>
</project>

lib-baby:

<project>
    <groupId>com.company.dev</groupId>
    <artifactId>lib-baby</artifactId>
    <packaging>jar</packaging>

    <parent>
        <groupId>com.company</groupId>
        <artifactId>lib-parent</artifactId>
    </parent>

    <profiles>    
        <profile>
            <id>release</id>

            <build>
                <plugins>
                    <!-- produces 'lib-baby-release.jar -->
                    <plugin>
                        <groupId>com.pyx4me</groupId>
                        <artifactId>proguard-maven-plugin</artifactId>
                    </plugin>

                    <!-- installs 'lib-baby-release.jar -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-install-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>install-release</id>
                                <phase>install</phase>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

lib-child:

<project>
    <groupId>com.company.dev</groupId>
    <artifactId>lib-child</artifactId>
    <packaging>jar</packaging>

    <parent>
        <groupId>com.company</groupId>
        <artifactId>lib-parent</artifactId>
    </parent>

    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>

            <dependencies>
                <dependency>
                    <groupId>com.company.dev</groupId>
                    <artifactId>lib-baby</artifactId>
                </dependency>
            </dependencies>
        </profile>

        <profile>
            <id>release</id>

            <dependencies>
                <dependency>
                    <groupId>com.company.dev</groupId>
                    <artifactId>lib-baby</artifactId>
                    <version>1.0</version>
                    <!-- made optional because will be embedded in standalone jar -->
                    <optional>true</optional>
                </dependency>
            </dependencies>

            <build>
                <plugins>
                    <!-- produces 'lib-child-release.jar -->
                    <plugin>
                        <groupId>com.pyx4me</groupId>
                        <artifactId>proguard-maven-plugin</artifactId>
                        <configuration>
                            <assembly>
                                <inclusions>
                                    <inclusion>
                                        <groupId>com.company.dev</groupId>
                                        <artifactId>lib-baby</artifactId>
                                    </inclusion>
                                </inclusions>
                            </assembly>
                        </configuration>
                    </plugin>

                    <!-- installs 'lib-child-release.jar -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-install-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>install-release</id>
                                <phase>install</phase>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文