0

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.

Brandon
  • 2,367
  • 26
  • 32
  • 2
    So you are claiming it's a bug of maven-sources-plugin? So please file in a jira http://jira.codehaus.org/browse/MSOURCES Apart from that my assumption is that your configuration of the `fmpp-maven-plugin` `target/generated-sources/net/mintern/primitive` seemed to be the problem. You should change that into `target/generated-sources/` but you have to give a package in your freemaker tempale. – khmarbaise Mar 20 '15 at 08:06
  • Your hunch was correct! When I added the `net/mintern/primitive` hierarchy to my `templates` directory (so that I could output directly to `target/generated-sources`), the `sources.jar` came out correct. If you want to turn your comment into an answer, I'll happily accept it. Otherwise, I'll add an answer myself in a few hours. – Brandon Mar 20 '15 at 08:29
  • Thank you very much for taking the time to check it out! I assumed that the plugins operated by looking at certain directories, instead of by hooking into one another. That took me way down the wrong path when I was trying to solve the issue myself. – Brandon Mar 20 '15 at 08:31

1 Answers1

1

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.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • Changing the location of my template source files so that I could use the default `outputDirectory` did, indeed, fix the problem. [Here's the diff](https://github.com/mintern-java/primitive/compare/1.2...ff241c3#diff-600376dffeb79835ede4a0b285078036L85). – Brandon Mar 20 '15 at 13:42