0

There is:

Parent pom:

<groupId>practice</groupId>
<artifactId>app</artifactId>
<packaging>pom</packaging>
<version>1.0</version>

<modules>
    <module>h2db</module>
</modules>

<build>
    <pluginManagement>
    ...
    </pluginManagement>
</build>

<dependencies>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.185</version>
    </dependency>
</dependencies>

Child pom:

<parent>
    <artifactId>app</artifactId>
    <groupId>practice</groupId>
    <version>1.0</version>
</parent>

<artifactId>h2db</artifactId>
<build>
    <finalName>h2db</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>h2_server.H2ConsoleServer</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.185</version>
    </dependency>
</dependencies>

In this example these is a parent pom that has 2 (or more) dependencies. Child pom has only one dependency that it needs. Is it possible for me to build a jar from this child pom module and include (scope:compile) it's single dependency into it? It builds a jar but does not include h2-dependency in the jar file. I've tried to use maven-assembly-plugin with jar-with-dependencies in the child pom but as a result it made a jar with all dependencies from the parent pom.

IgorZ
  • 1,125
  • 3
  • 20
  • 44

2 Answers2

1

When listing dependencies in the parent pom, it is because they are needed for the child projects.

You should consider removing the dependencies that are not needed by all the children from the parent pom.

One best practice however, would be to use dependencyManagement. You could in the parent pom specify the version of any log4j and h2 libraries that would be added by a child project. Then in your child project you simply import the dependency without version.

Parent pom:

<groupId>practice</groupId>
<artifactId>app</artifactId>
<packaging>pom</packaging>
<version>1.0</version>

<modules>
    <module>h2db</module>
</modules>

<build>
    <pluginManagement>
    ...
    </pluginManagement>
</build>

<dependencyManagement>
<dependencies>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.185</version>
    </dependency>
</dependencies>
</dependencyManagement>

Child pom:

<parent>
    <artifactId>app</artifactId>
    <groupId>practice</groupId>
    <version>1.0</version>
</parent>

<artifactId>h2db</artifactId>
<build>
    <finalName>h2db</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>h2_server.H2ConsoleServer</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>
</dependencies>
YMomb
  • 2,366
  • 1
  • 27
  • 36
0

Off the top of my head, you can try the maven-shade-plugin (http://maven.apache.org/plugins/maven-shade-plugin/examples/attached-artifact.html)

Not sure whether it's exactly what you need, but it does allow you to specify dependencies to include/ignore.

Pseudos
  • 111
  • 1
  • 7
  • What if I have 100 dependencies should I ignore each of it instead of including only that I need? – IgorZ Jan 05 '17 at 11:28
  • 1
    Hmm... My knowledge of the plugin isn't 100%, but I believe you can at least use wildcards on packages? Seems you can also create a whitelist and presumably it will ignore the rest? http://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html shows how to set up includes. There's also a `minimizeJar` thing that COULD throw out unused stuff. The answer on http://stackoverflow.com/questions/8698814/configure-maven-shade-minimizejar-to-include-class-files explains a bit about that. – Pseudos Jan 05 '17 at 11:41
  • Thanks for the minimizeJar. I think I can make it with it's filters. – IgorZ Jan 05 '17 at 13:03