4

I want to build a maven jar artifact from classes. I don't have source files. These classes are originally in another artifact installed locally. I use maven-dependency-plugin to unpack the classes and put them in the target folder for this project/module.

It creates the jar.. but doesn't include the classes I just unpacked. Here's my pom:

<build>
... 

<!-- unpack myjar1.jar and myjar2.jar -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <executions>
                    <execution>
                        <id>unpack</id>
                        <phase>package</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.company</groupId>
                                    <artifactId>myjar1</artifactId>
                                    <version>1.0</version>
                                    <type>jar</type>
                                    <overWrite>false</overWrite>
                                    <outputDirectory>target/final</outputDirectory>
                                </artifactItem>
                                <artifactItem>
                                    <groupId>com.company</groupId>
                                    <artifactId>myjar2</artifactId>
                                    <version>1.0</version>
                                    <type>jar</type>
                                    <overWrite>false</overWrite>
                                    <outputDirectory>target/final</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>default</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <classesDirectory>/path/to/target/final/folder</classesDirectory>
                            <includes>
                                <include>**</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

</plugins>

</build>

How can I include these classes into my final.jar?

Aravind Datta
  • 327
  • 4
  • 6
  • 17

3 Answers3

3

I think the best solution is the maven-shade-plugin: create a pom.xml, add those 2 libraries as dependencies and configure the maven-shade-plugin. Run mvn package and you have your merged project.

Robert Scholte
  • 11,889
  • 2
  • 35
  • 44
2

What Robert wrote above might be a workable solution too.. but I figured a different way out. I simply removed <includes> inside the maven-jar-plugin and it worked. I ran the build in eclipse by creating a build configuration and chose "debug" option. It spit out a lot of info about "configuration" which is otherwise not displayed.

Thanks!

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>default</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <classesDirectory>path/to/final/folder</classesDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

This worked!

Aravind Datta
  • 327
  • 4
  • 6
  • 17
2

Another approach is to set ouputDirectory to regular target/classes directory. target/classes

So that unpacked classes plus your project classes will be avaialble in target/classes which can be bundled in to .jar using regular maven-jar-plugin by specifing **

Complete pom:

    <build>
<plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.8</version>
            <executions>
                <execution>
                    <id>unpack</id>
                    <phase>package</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>a.b.c</groupId>
                                <artifactId>aaa</artifactId>
                                <type>jar</type>
                                <overWrite>true</overWrite>
                                <outputDirectory>target/classes</outputDirectory>                                    
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <id>default</id>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>

                    <configuration>
            <includes>
                    <include>**</include>
            </includes>
            </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
Sudheer Palyam
  • 2,499
  • 2
  • 23
  • 28