2

I created a pom.xml to compile my project and package it as a jar, and indeed it comiples and the jar is created - problem is that i got a jar with both classes and java inside, and i only want the classes inside.

How do I lose the java files? I don’t need them.

this is the pom.xml that i created:

<build> <resources> <resource> <directory>src/main/java</directory> <filtering>true</filtering> </resource> </resources> <finalName>api-interfaces</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <executions> <execution> <id>make-a-jar</id> <phase>compile</phase> <goals> <goal>jar</goal> </goals> </execution> </executions> <configuration> <excludes> <exclude>*.properties</exclude> <exclude>*.xml</exclude> <exclude>sql/**</exclude> <exclude>META-INF/**</exclude> <exclude>*.jar</exclude> <exclude>*.java</exclude> <exclude>default-configs/**</exclude> </excludes> </configuration> </plugin> </plugins> </build> 

this is the jar that i get

2 Answers 2

7

Here's the culprit:

 <resources> <resource> <directory>src/main/java</directory> <filtering>true</filtering> </resource> </resources> 

With this, you explicitly specify that files in src/main/java, i.e. the .java files, should be included in the jar.

you can either use the standard maven layout and put resources in src/main/resources, or explicitly exclude .java files using this:

 <resources> <resource> <directory>src/main/java</directory> <filtering>true</filtering> <excludes> <exclude>**/*.java</exclude> </excludes> </resource> </resources> 

See maven resource plugin for more infos, especially the include/exclude examples

Sign up to request clarification or add additional context in comments.

2 Comments

The solution was indeed to exclude the java: <exclude>**/*.java</exclude> and not as i did before: <exclude>*.java</exclude>
I take it you added this to the maven-jar-plugin configuration? Note that by doing so, you only remove the files from the generated jar file. Any other plugin that works with the project's resources will still see them. In most cases, this won't make a difference, but it's something to look out for.
2

Is there a good reason for binding the maven-jar-plugin:jar goal to the compile phase explicitly? The compile phase is intended to "compile the source code of the project", nothing else.

In a jar packaging project the jar:jar goal is bound to the package phase by default which "take the compiled code and package it in its distributable format, such as a JAR."

The resources:resouces goal is bound to the process-resources phase which "copy and process the resources into the destination directory, ready for packaging."

Binding default goals to non-default phases is more like working against Maven than with it.

By using src/main/resources as mentioned by @SillyFreak in his answer you probably don't need this build step definition in your POM at all.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.