0

I'm having a hard time adding java source files and the attached dependencies to an output jar file using maven.

What I want to archive is having the compiled classes, the source files and all dependencies (without sources) in one jar which can be distributed.

- com/name/project/*.class
- com/name/project/*.java
- lib/*.jar
- META-INF/MANIFEST.MF  

Here is the pom.xml what I have so far:

 <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.7</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.name.project/main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

I hope anyone can help me.

cheers dan

thesonix
  • 3,200
  • 4
  • 20
  • 19
  • Let me say in short. Wrong way. Use the usual Maven ways which supports already having a source, javadoc package etc. – khmarbaise Apr 05 '13 at 09:30

3 Answers3

2

I decided not to add the dependecy-jars to my output jar. Instead I used the following pom.xml to include my sources to the jar alongside with the compiled dependencies. I had to exclude the */.java files due to some strange dependencies having the sources included as well.

<build>
    <sourceDirectory>src/</sourceDirectory>
    <resources>
        <resource>
            <directory>${project.build.sourceDirectory}</directory>
        </resource>
        <resource>
            <directory>${basedir}/src/main/resources</directory>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.7</version>
            <executions>
                <execution>
                    <id>unpack-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>unpack-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/classes</outputDirectory>
                        <excludes>**\/*.java</excludes>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

thanks for the help.

cheers dan

thesonix
  • 3,200
  • 4
  • 20
  • 19
1

A JAR usually really shouldn't contain other JARs, but of course there's nothing to prevent you from doing that.

You can include the source code neatly using the resource section as follows:

<build>
    </resources>
        <resource>
            <directory>src/main/java</directory>
        </resource>
    </resources>
    ...

I'd venture a guess that you might be able to bind your copy-dependencies task to the resources phase and see if that would include the dependency JARs in the final artifact.

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
0

Maybe you should see this link. Just check you have correctly configured your maven+IDE co-operation

Community
  • 1
  • 1
John Smith
  • 831
  • 5
  • 19
  • 37