The only issue with this approach is that the final artifact includes the java source files as well(.java files). Is there a way to exclude the source files and include only have the .class files?
– saravana_pcSep 05 '12 at 09:23
19
just a note for others (like me), `plugin` element is in `/project/build/plugins` and NOT in `/project/build/pluginManagement/plugins`
– BetlistaSep 10 '12 at 09:20
So if (in Eclipse Luna) i added (linked) in my BuildPath (clicking the right mouse button on my MavenProject) a source folder where its directory is "/Users/MyName/Desktop/source", i have to write `/Users/MyName/Desktop/source` ?? And everything goes right?
– AeroxApr 29 '15 at 18:03
If you also have tests in a seperate source folder (e.g. src/test) , do not forget to also add your generaged sources to the test-compile.
You can do this, by adding another `` with the `add-test-source` and `generate-test-sources`.
– Peter IttnerJul 10 '15 at 06:59
3
If you are using eclipse, you might want to install `m2e connector for build-helper-maven-plugin` from eclipse marketplace to remove the error in pom.xml
– dieendSep 23 '15 at 07:57
1
If you receive a warning like `'build.plugins.plugin.version' for org.codehaus.mojo:build-helper-maven-plugin is missing` you need to add within `` the tag `1.12`
– AlphaaaOct 20 '16 at 11:47
This worked and was understood by IDEA Maven project import. I had to try reimport in IDEA a couple times to get IDEA module structure updated.
– Dima KorobskiyJun 20 '17 at 19:39
This worked for me with Maven 3.5.2, but I had to set it to `validate`.
– KentAug 06 '18 at 21:08
9
So the best way to do this, in 2017, was to create an XML pasta. Does nobody see a problem with that?
– TomOct 11 '18 at 04:25
@saravana_pc I only have class files (no .java files) in final artifact. (build-helper-maven-plugin 3.0.0)
– bjmiFeb 09 '20 at 09:51
Worked for me to :) Eclipse doesn't seem to like it though. It seems to think "src/main/java, src/interfaces" is a single src, and therefore flags it as (missing).
– JoelSep 10 '16 at 19:29
I got similar experience as @Joel, Code compiled but had problems with things like recognizing a "main" method.
– JohnApr 13 '21 at 23:46
4
For maven 3.8.1 does not work. Also, in the documentation for this version of the maven, nowhere is it written that such a content is possible in the tag .
--- maven-compiler-plugin:3.8.1:compile (default-compile) @ xxx-xxx ---
No sources to compile
– EliahuMay 31 '21 at 10:24
Not such a good idea IMHO, since several plugins assume the `sourceDirectory` - and possibly additional `sources` - as the roots of source files. In your solution, the `maven-compiler-plugin` is the only plugin aware of these actual roots.
– Laurent PireynMay 04 '11 at 14:17
3
@Laurent You're right about that. This was a good idea a couple years ago but there are much better options now. build-helper listed above is my preferred options.
– salMay 06 '11 at 01:51
This cannot work if i want to include an external source directory (containing Java class that i'm using in my maven project). What if my external source is located outside my Eclipse's workspace? What can i do?
– AeroxMay 01 '15 at 15:05
Would like to add that this worked in Eclipse as well to add the generated sources as a source location in the project configuration.
– Adam HawkesAug 14 '13 at 16:58
3
This path seems to be for sources generated by annotation processors though. Even if it works, it is possible that this path is handled differently by some plugins. For example I would expect this directory could be deleted when 'clean' is run.
– kapexMar 06 '18 at 10:55
**IntelliJ 2021.2.3** This still works and it was the only solution I found that did. I agree with @kapex that it is intended for annotations, but I spent a day trying to find the "right way" to do it and failed.
– cb4Dec 12 '21 at 22:09
To make the build less brittle and more in line with maven standards:`${project.build.directory}/generated-sources/main/java`
– cb4Dec 12 '21 at 22:18
9
This also works with maven by defining the resources tag. You can name your src folder names whatever you like.
Used the build-helper-maven-plugin from the post - and update src/main/generated. And mvn clean compile works on my ../common/src/main/java, or on ../common, so kept the latter. Then yes, confirming that IntelliJ IDEA (ver 10.5.2) level of the compilation failed as David Phillips mentioned.
The issue was that IDEA did not add another source root to the project. Adding it manually solved the issue. It's not nice as editing anything in the project should come from maven and not from direct editing of IDEA's project options. Yet I will be able to live with it until they support build-helper-maven-plugin directly such that it will auto add the sources.
Then needed another workaround to make this work though. Since each time IDEA re-imported maven settings after a pom change me newly added source was kept on module, yet it lost it's Source Folders selections and was useless. So for IDEA - need to set these once:
Select - Project Settings / Maven / Importing / keep source and test
folders on reimport.
Neither option works with IntelliJ Idea 9.0.4 which is what I use. Haven't tried the build-helper options with the recent Eclipse, but it didn't work with 3.4 and the m2 plugin when I tried it. Maven doesn't like multiple source trees or multiple artifacts built from the same project any any attempt to get around this limitation is usually an awful hack.
– salNov 11 '11 at 14:16
Have been on IntelliJ for many years now. And never switched to eclipse, so can't talk for it, then hearing it's generally very good too. For IntelliJ Upgrading a personal license every other year is at $100/year. The new major versions are usually out every year at January. Then at the last 2-3 month of previous year they are allowing you to buy the previous version and get the upgrade to the upcoming one free. This is on right now so it's the "safe" time to buy 10 and get 11. Also, if you don't need JSP and other enterprise features, use the free community edition.
– arntgNov 11 '11 at 17:16
1
This can be done in two steps:
For each source directory you should create own module.
In all modules you should specify the same build directory: ${build.directory}
If you work with started Jetty (jetty:run), then recompilation of any class in any module (with Maven, IDEA or Eclipse) will lead to Jetty's restart. The same behavior you'll get for modified resources.
these are all the configurations available for 3.8.1 version of compiler plugin. Different versions have different configurations which you can find by running your code with -X after the general mvn command. Like
mvn clean install -X
mvn compiler:compile -X
and search with id or goal or plugin name
This may help with other plugins too. Eclipse, intelliJ may not show all configurations as suggestions.