I have a situation where I need to have a folder containing Java sources used as a source folder for several maven projects "next to each other" in a tree structure. Due to dependency differences for the maven projets I cannot create an artifact containing the compiled version of the sources, but need to have each project treat it as a source folder in addition to src/main/java.
Apparently Maven can do this easily by adding another source folder located in "../foo/src", but m2e refuses to do this, and for this to work well for us, I need to have it working in Eclipse.
How would I go at having a structure like:
/common/src
/a/pom.xml (add source folder ../common/src)
/a/src/main/java/...
/b/pom.xml (add source folder ../common/src)
/b/src/main/java/....
and get it working in Eclipse?
(note: I am aware of http://dev.eclipse.org/mhonarc/lists/m2e-users/msg01988.html - it is, however, from 2011)
You should not do that.
If you want to use that code in different modules, then it should also be a Maven module, used as a dependency by the other modules.
The main problem with what you're trying to do is that even though it is not an actual copy/paste of sources between the two modules, in the end it behaves like one. What would happen once you build the two jars? You'll have duplicate classes, so if you use them in the same application the classpath will be a bit wrong.
So, what exactly is it that you're trying to accomplish?
If you really, really have to keep it as a shared source directory instead of a shared dependency, then you can look at this answer.
How about a little trick with the file system? Just make symlinks to the folders and you probably be fine :)
For NTFS you can try to do mklink
from command line.
More explanation here: http://en.wikipedia.org/wiki/NTFS_symbolic_link
You should be able to use relative paths and Maven Build Helper as a solution.
In each project, or in a "parent" pom.xml that they all inherit from, add the following:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/../../common/src</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
If you use Subversion probably the most convenient approach would be to keep the shared source folder in a separate repository and add it to all the projects that need it by means of svn:externals
. On the other hand this would make creating tags and branches more complicated.
Something similar could probably be achieved with Mercurial sub-repositories, but it wouldn't be as convenient.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With