Search code examples
javamavenjargenerated-codemaven-source-plugin

maven-source-plugin builds sources.jar, but it's unusable by IDEs


I have a Maven project that's built from generated sources. I'm using the maven-source-plugin, configured as follows:

<profiles>
    <profile>
        <id>release</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <version>2.4</version>
                    <executions>
                        <execution>
                            <id>attach-sources</id>
                            <goals>
                                <goal>jar-no-fork</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                ...

I build using mvn clean install -P release. This produces a ${artifactId}-${version}-sources.jar file that includes all the source files, but apparently it's packaged incorrectly, because IDEs do not recognize that the source is available. Everything else builds and works correctly, including the Javadocs.

The main entry point for the library is net.mintern.primitive.Primitive. When the project builds, the Java file is generated at target/generated-sources/net/mintern/primitive/Primitive.java. In sources.jar, however, Primitive.java (and everything else in the primitive directory) is at the root of the JAR. The parent directories (net/mintern/primitive) are absent from the JAR.

Is this normal, or might this be the cause of my problem? I tried different includes, changing to jar instead of jar-no-fork, explicitly adding <attach>true</attach>, using build-helper-maven-plugin to add-source, and probably more that I'm forgetting—nothing had any effect. The Maven Source Plugin configuration doesn't seem to provide any way to tweak the path within the JAR.

Can you help me figure out what's going on here? You can include the project in your POM by adding this dependency:

<groupId>net.mintern</groupId>
<artifactId>primitive</artifactId>
<version>1.2</version>

The full POM can be viewed on GitHub, and if you're willing to try building it, you can check out the problematic project with git clone https://github.com/mintern-java/primitive.git, and then git checkout 1.2.

EDIT: I managed to hack around the problem using an ungodly combination of maven-jar-plugin and build-helper-maven-plugin; that POM is version 1.2.1. I'm still interested in a proper maven-source-plugin fix, though.


Solution

  • Apart from that my assumption is that your configuration of the fmpp-maven-plugin:

    <outputDirectory>target/generated-sources/net/mintern/primitive</outputDirector‌​y>
    

    seemed to be the problem. You should change that into:

    <outputDirectory>target/generated-sources/</outputDirectory> 
    

    but you have to give a package in your freemaker template. Furthermore you should check the docs of freemarker plugin to see if the outputDirectory isn't configured by default with the above value. If so you can completely remove this entry from your pom. (Follow the defaults). Convention over configuration.