1

short: I need to filter all .java files and every META-INF folder from a set of jars and package the class files and resources from that jars into one single jar. I currently use the maven-assembly-plugin, but would try something else as long as it can easily be integrated into a maven build process.

long: I use maven to manage different stages of development for my tool. basic stage is freeware, second has some more features, third stage is all features)

That works fine so far, I use profiles to add the different source directories to the classpath and the sources are neatly compiled into the project.jar.

  • First problem: The .java sources included into the project via the profiles end up in the project.jar.

Then I use the maven-assembly-plugin to construct a final.jar that also contains the dependencies and in the end use launch4j to produce an executable for windows (the current target platform).

  • Second problem: The various META-INF parts from the dependency jars mix in the final.jar and I would want them all to be skipped.

I have searched for examples of the assembly.xml using the <exclude> tag, but did not find any that used my combination of dependencySet and <exclude>*.java</exclude>. I'm not even positive that I can do that.

Here is my assembly.xml:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>jar-with-dependencies</id>
    <formats>
        <format>jar</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>/</outputDirectory>
            <useProjectArtifact>true</useProjectArtifact>
            <unpack>true</unpack>
            <scope>runtime</scope>
            <useDefaultExcludes>true</useDefaultExcludes>
            <!--<useTransitiveFiltering>true</useTransitiveFiltering>-->
            <!--<useStrictFiltering>true</useStrictFiltering>-->
            <excludes>
                <exclude>META-INF</exclude>
                <exclude>**/*.java</exclude>
                <exclude>*.java</exclude>
                <exclude>*:sources</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
</assembly>

My research so far:

I have googled with example assembly.xml exclude java but could not find examples that covered my problem. (I have also googled a lot the past days but did not save all I found) I have read http://maven.apache.org/plugins/maven-assembly-plugin/advanced-descriptor-topics.html but could not apply that knowledge to my problem.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72

2 Answers2

1

Okay, so I figured it out for me.

first: to filter out java and other source files from source parts that were included using profiles I use:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>de.steamnet.oneClickWonder.awt.OCWController</mainClass>
                    </manifest>
                </archive>
                <excludes>
                    <exclude>**/*.java</exclude>
                    <exclude>**/*.form</exclude>
                </excludes>
            </configuration>
        </plugin>

The task of filtering the META-INF from the dependencies has gone away when I started using an installer so now I can just deliver mulitple jars with their own META-INF.

So, as Michael-O stated this approach (using profiles to include additional source parts) may not be the correct one to do but it is very handy and I stick to it. With the excludes tag from the jar plugin the troubles with source files being added to the final jar also goes away.

Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72
-1

This is absolutely the wrong way to go. Do never produce more than one main jar per artifact id. You should use this approach:

  1. create for every main jar a single module.
  2. set dependencies from say full to basic and so forth.
  3. now you have to options to repackage your stuff. You either use the shade plugin which will include the artifact the way you want/need or you use the dependency:unpack-dependencies and put the stuff in the output dir.

After that your will end up with the "aggregated" jar files. Doing that with profiles and asm plugin simply does not scale.

After you have done that create another module for the tar.gz or your distro and depend on that main jars and bundle up your assembly. You are done!

Viel Erfolg und Gruß nach Düren

Michael-O
  • 18,123
  • 6
  • 55
  • 121
  • Hmm... well, I thought about that but had already decided against it. It is in fact so much more work that just using profiles and loading the correct source branches than it is to build every single piece of it into a single module. Thank you very much for your input, but this is no viable way to go in my situation. I think I will shift to write a shellscript for creation of the exe and filter out what is too much before that manually. Vielen Dank trotzdem, und Grüße nach Berlin – Angelo Fuchs Aug 07 '11 at 10:06
  • In my opinion, you approach is too complicated and ill-designed. It should rethink it. – Michael-O Aug 07 '11 at 10:09
  • It is ill-designed. But i have not yet found a better approach. Using different modules just creates too much day-to-day overhead that I want to avoid. I would end up having 5 or 6 modules (because some parts of the lowest stage are not part of the higher ones etc.). That would be just plain confusing. In the current setup I choose a profile and see exactly the parts of the code at once that I'm working on. The problems that brought me here are smaller. (btw. I edited my original comment a bit) – Angelo Fuchs Aug 07 '11 at 10:14
  • You could do the same from the parent with profiles enables modules or run `mvn -pl `. This is what I do. I have per target server one module. This is easier than profiles and more obvious. – Michael-O Aug 07 '11 at 10:32
  • Okay, I'll have a look. Thank you. – Angelo Fuchs Aug 07 '11 at 10:36