-1

I have a maven project which should produce several binaries. Some of them have dependencies others dont and the dependencies may differ from binary to binary.

The question is: How do I achieve this without including every dependency but only the necessary ones into every jar?

Currently I use the assembly plugin which does the job except that it puts every dependency into every jar. Additionally it creates the standard app1-snapshot jar which I do not need. How can I get rid of it?

My pom file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>antlrtest</groupId>
    <artifactId>app1</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <id>thf_test</id>
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>parsers.THF_test</mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                            <finalName>bin/THF_test</finalName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>hmf_test</id>
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>parsers.HMF_test</mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                            <finalName>bin/HMF_test</finalName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>HMF_to_THF</id>
                        <phase>package</phase> <!-- bind to the packaging phase -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <archive>
                                <manifest>
                                    <mainClass>transformation.HMF_to_THF</mainClass>
                                </manifest>
                            </archive>
                            <descriptorRefs>
                                <descriptorRef>jar-with-dependencies</descriptorRef>
                            </descriptorRefs>
                            <finalName>bin/HMF_to_THF</finalName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <!-- antlr -->
            <plugin>
                <groupId>org.antlr</groupId>
                <artifactId>antlr4-maven-plugin</artifactId>
                <version>4.5</version>
                <configuration>
                    <arguments>
                        <argument>-package</argument>
                        <argument>parsers</argument>
                    </arguments>
                </configuration>
                <executions>
                    <execution>
                        <id>antlr</id>
                        <goals>
                            <goal>antlr4</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.antlr</groupId>
            <artifactId>antlr4-runtime</artifactId>
            <version>4.5</version>
        </dependency>
        <dependency>
            <groupId>org.antlr</groupId>
            <artifactId>antlr4-maven-plugin</artifactId>
            <version>4.5</version>
            <type>maven-plugin</type>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>
Waschbaer
  • 447
  • 6
  • 18
  • Related (but not duplicate) to http://stackoverflow.com/questions/2424015/maven-best-practice-for-generating-multiple-jars-with-different-filtered-classes – YMomb Sep 20 '16 at 15:12

1 Answers1

0

As explained in https://stackoverflow.com/a/2426271/800413, it's often a bad practice to generate multiple artifacts/binaries from the same project.

In your case, why not have multiple projects sharing the main code base as a dependency and producing the required binaries, with appropriate libraries per project?

Community
  • 1
  • 1
YMomb
  • 2,366
  • 1
  • 27
  • 36
  • Hmm yes I could do that. Is there an option to build multiple mvn projects simultaneously with one command? In this case there should be some libraries (e.g. as jars) in a lib directory and some binaries in a bin directory. – Waschbaer Sep 20 '16 at 16:53
  • You can create a maven multi-project stucture where you have a main project and modules. When calling any command at main project level, this will apply to all sub-projects and thus you can build plenty of projetcs in a single `mvn package` command – YMomb Sep 21 '16 at 07:03