3

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?

stephanos
  • 3,319
  • 7
  • 33
  • 47

2 Answers2

2

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/

Community
  • 1
  • 1
Martin Algesten
  • 13,052
  • 4
  • 54
  • 77
  • thanks for the reply, but not really helpful; I already described almost everything you mention (standalone JAR, profiles) - the question probably sucked, added my own answer (90% happy with it) – stephanos Oct 24 '11 at 13:11
0

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>
stephanos
  • 3,319
  • 7
  • 33
  • 47