How can I configure Maven (command line) and the IDE to build in different folders? - eclipse

I'm often switching between my IDE (running just the unit tests for the code which I'm working on) and the command line (running all unit tests).
This causes problems: Sometimes, mvn clean fails because the IDE is building the code. Or I get weird errors in the IDE about missing classes because the IDE is reading files which Maven has modified.
Also, I can't continue working in the IDE why Maven builds in the background.
How can I configure my IDE or Maven to use different build folders?

Use profiles and this code in your (parent) POM:
<project>
...
<build>
<outputDirectory>${basedir}/${target.dir}/classes</outputDirectory>
<testOutputDirectory>${basedir}/${target.dir}/test-classes</testOutputDirectory>
</build>
<properties>
<target.dir>target</target.dir>
</properties>
<profiles>
<profile>
<id>ide-folders</id>
<properties>
<target.dir>target-ide</target.dir>
</properties>
</profile>
</profiles>
...
Configure the project in your IDE to enable the profile target-ide when building. Now Maven and the IDE use different folders.
Alternatively, you could enable this profile in your settings.xml and make the folder target-ide the default. This way, you wouldn't have to modify the settings of many projects in your IDE. It would even work on your CI build server (it would build into target-ide but who cares? And if you cared enough, you can enable the profile there as well).
Note: This is based on a blog-post on jroller.com which is down. The Internet Archive has a copy: https://web.archive.org/web/20150527111413/http://www.jroller.com/eu/entry/configuring_separate_maven_output_folders

Related

Maven profiles are not considered in Eclipse

I have three profiles defined in my pom.xml:
<profiles>
<profile>
<id>ABC</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<url.base>http://server1.de</url.base>
<url.searchevse>/search</url.searchevse>
<url.reservation>/reservation</url.reservation>
<url.cancelation>/reservation/cancel</url.cancelation>
<xxx.devmode>false</xxx.devmode>
</properties>
</profile>
<profile>
<id>XYZ</id>
<properties>
<url.base>http://server2.de</url.base>
<url.searchevse>/cns/search</url.searchevse>
<url.reservation>/cns/reservation</url.reservation>
<url.cancelation>/cns/cancel_reservation</url.cancelation>
<xxx.devmode>false</xxx.devmode>
</properties>
</profile>
<profile>
<id>DEVELOPMENT</id>
<properties>
<url.base>http://localhost/noservices</url.base>
<url.searchevse>/no/search</url.searchevse>
<url.reservation>/no/reservation</url.reservation>
<url.cancelation>/no/cancel_reservation</url.cancelation>
<xxx.devmode>true</xxx.devmode>
</properties>
</profile>
</profiles>
In Eclipse I have a Run Configuration
clean install XYZ
and I tried both using -PXYZ (and -P XYZ) in the Goals field as well as
clean install
in the Goals field and XYZ in the Profiles field.
The problem:
The defined profile is never used.
Inserting the active profile under Properties-->Maven-->Active Maven Profiles doesn't work (or do I have to use a special syntax, e.g. no spaces after a comma or so).
Right click your project in the Project Explorer then go to
Properties --> Maven --> Active Maven Profiles
and type in only the profile name that you want to run.
If you want to run your ABC profile, type in ABC in the Active Maven Profile input box.
The description(separated by commas) given there is a bit confusing.
Once you define your profile name or id in the input box. You can clean and run your project on your server.
By doing so, your mentioned active maven profile will be run.
verify that plugin execution id are not equal in different profiles
For those who still have problems. I am not really used to Eclipse nor STS but I actually use Spring Tool Suite Version: 4.7.1.RELEASE. I think the Active Maven Profile input box. may be named Write here your already activated Maven Profile :) as I think its purpose is to select an activated profile to run.
In my case, what I did was :
edit my Maven settings.xml (maven folder, conf folder) file to activate the desired profile with <activeProfile>MyProfileId</activeProfile> in the activeProfiles section. The one thing I really appreciate is that it accepts profile even defined in your project pom.xml and that is great for peoject with sub modules. If you have time, read this https://maven.apache.org/guides/introduction/introduction-to-profiles.html
Then add your profile to Active Maven Profile input box (Ctrl+Alt+P or right click on your project, Maven, Select Maven profiles). You can also deactivate a profile with ! or - before a profile name.
You might need to restart Eclipse.
The first step is only to activate your profile. So if you have any way to achieve this, feel free to do as so.
In Eclipse 4.20 you can right click on project -> Maven -> Select Maven Profiles...

Eclipse and m2e - test classpath

I have Spring project backed up by maven and I am using m2e plugin. Now, some of my files are in the /src/main/webapp/WEB-INF/ folder. Now, the problem is that when I am running tests some of the .xml files with configuration don't work because paths to the resources are breaking.
Firstly, question. When I do on project Run As -> JUnit test, dose eclipse backs up by doing mvn test, or is it maven agnostic this way? If so this would explain why after adding
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>/src/main/webapp/</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
to my pom.xml when I run the test using Run As -> JUnit test and printing classpath I am not seeing my added location, but doing mvn test I can see this location added to classpath.
So, is it possible to make eclipse/maven copy some additional files to target/classes or target/test-classes
or
just add some folders to the runtime classpath when testing the applcation.
Also, is it possible to do such thing in one place so it does not matter if I am running tests using Eclipse gui or just mvn test from console.
Unless this has changed recently, your class path in eclipse is still built from your .classpath file.
m2e injects some elements, notably dependencies, but source/resource paths still need an entry.
Adding what you need to your .classpath should solve the issue.

Eclipse/Maven: JUnit tests not compiled when running them

I am working on a project using Maven and Eclipse (m2eclipse plugin). I've got problems with the JUnit tests:
Sometimes, when running them within Eclipse, they wont be compiled, but the old class files are used instead. When I delete the class files, I get ClassNotFoundExceptions in Eclipse. I then have to manually recompile them by using mvn test-compile or other goals.
I also noticed that the class files of the tests sometimes are put into the classes subdirectory instead of test-classes.
I really can't figure out what is wrong.
The JUnit java files are within src/main/java and are correctly named (*Test.java).
Do I have to compile and run them always via Maven? Why doesn't Eclipse compile the files when I want to run them? (Interestingly, sometimes it does. Sometimes everything works perfectly.)
I had the same problem with STS Eclipse (Spring development variant), m2e and JUnit. The solution was to set the output folder for src/test/java to target/test-classes:
Right click the src/test/java folder in the Package Explorer
Select Build Path -> Configure Output Folder
Enter target/test-classes, click OK
Now the changes in test classes are compiled correctly and you should be able to run JUnit tests in Eclipse.
The problem is that Eclipse compiles the unit tests to the default output folder target/classes while JUnit plugin correctly tries to run them from test-classes.
There are a few duplicates to this question:
ClassNotFoundException when running JUnit unit tests within Eclipse (using Maven)
Eclipse doesn't see my new junit test
junit not using the newest file
In addition to the answer below
Right click the src/test/java folder
Select Build Path -> Configure Output Folder
Enter target/test-classes, click OK
you should check to ensure that your builder is setup correctly by right clicking your project and going to Properties -> Builder. If you see that your builder is missing, you need to install one. In my case, the maven project had an AspectJ dependency and when I used the Maven Eclipse plugin to build my Eclipse project, it was looking for an AspectJ builder by default. I installed the AspectJ development tools and it solved the problem.
Hope this helps!
The most likely explanations for the problem you are facing is that the output folder of src/test/java is not correctly configured.
Instead of fixing this configuration manually, you can have m2eclipse fix this for you: Just right-click on the project and choose Maven > Update Project.
And another point: JUnit test classes should be in src/test/java, not src/main/java, otherwise they aren't detected correctly by Maven as test classes and they would be included in the packaged jar and not in the test jar.
I faced same issue. Tried above suggestions of configuring output folder & Maven>Update Project but neither worked. Finally changed my testOutputDirectory to "build/classes" as well and now Unit Tests are getting picked up and executed.
Finally found the reason for the issue. In my pom we had also configured maven compiler plugin as below
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<outputDirectory>build/classes</outputDirectory>
</configuration>
</plugin>
outputDirectory configuration is not needed and was the cause of above issue. After removing this tag, junits are getting compiled to build>testclasses folder and are being run during maven build as well. Yippee :)
Please check "testSourceDirectory" path which can be configured in your
pom.xml. And then, Add the folder (configured in "testSourceDirectory" path) to the eclipse build path.
Please find the sample "testSourceDirectory" in pom.xml below:
<build>
<testSourceDirectory>src/test/java</testSourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
Make sure that is there any exclamation mark on your project icon! In my case, i ignored that there is a exclamation point like:
exclamation point on project icon
Open the "Markers" perspective, then troubleshoot the problems according to the tips.
what the "Markers" perspective show
The junit test classes can be execute successfully after i called "mvn clean test" because they are not refer the unreadable jar which be warned in "Markers" perspective.Therefor, it's easily to ignore it..
For someone working on java-scala mix project, this is something to note.
Even after doing the configuration in the manner shown below,
<build>
<testOutputDirectory>${basedir}/target/test-classes</testOutputDirectory>
<plugins>
...
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.3.2</version>
<configuration>
<testSourceDir>${basedir}/src/test/scala</testSourceDir>
<testOutputDir>${basedir}/target/test-classes</testOutputDir>
</configuration>
</plugin>
</plugins>
</build>
and doing a Maven > Update Project, eclipse honours the output directory of the src/test/java folder in the project, however, not for the src/test/scala folder. (You can figure this out by right-clicking on the specific source folder and choosing Build Path > Configure Output Folder... which should display the location as specified in the pom for the former case, however, not for the later case.
This is already a known bug for using scala with m2e as mentioned here: http://scala-ide.org/docs/tutorials/m2eclipse/
Warning
As of March 2013, a bug causes both src/main/scala and src/test/scala to use the default output folder (target/classes). This may be confusing >when building tests, since their class files will not end in target/test-classes, as they would when building on the command line. You can work around this by manually changing the output folder for src/test/scala.
Eclipse is not expecting anything else to be mucking with the class files. It assumes that if you haven't editted the file in eclipse it hasn't changed and doesn't need compiling. I think the issue stems from eclipse and maven sharing an output directory. I've often seen this if my mvn build fails, it will have deleted the class files as part of the clean but not compiled new ones. I think the best solution would be to have seperate build dirs for mvn and eclipse, but I've never look into this.
My problem wasn't the JUnit plugin but rather the configuration in my pom.xml.
After reviewing all the answers to this question, #Gulats's answer implied to me that I should try setting a testOutputDirectory in my maven-compiler-plugin section, and that did the trick:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<testOutputDir>${basedir}/target/test-classes</testOutputDir>
</configuration>
</plugin>

Getting Jetty & Maven to work form TextMate

I often work in Eclipse, but recently switched to TextMate, which is, in my opinion, the best text editor out there, barring perhaps VIM or something like that (but I find the learning curve too steep to jump into that quite yet).
The disadvantage is I don't really know how to run Maven & Jetty w/out using Eclipse. The engineers at work here set up Java projects that have POM files, etc. And it all runs fine in Eclipse, but I thought I heard somewhere that TextMate actually had a bundle somewhere to use Maven with, and I wondered if such a thing might include Jetty too.
I'm a UI guy, so please, keep the technical jargon down just a tad. (In other words, I don't do Java, though I understand some of it.)
You don't need anything fancy to run a Mavenized webapp on Jetty. Simply add the following snippet to your pom.xml (assuming your project has a packaging of type war as illustrated below):
<project>
...
<packaging>war</packaging> <!-- this is the packaging for a webapp -->
...
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.22</version>
</plugin>
...
</plugins>
...
</build>
</project>
And just run the following command in a console:
mvn jetty:run
Just in case, there is a Maven bundle for TextMate allowing to invoke Maven from TextMate. You can check this mail for details.

Get source jar files attached to Eclipse for Maven-managed dependencies

I am using Maven (and the Maven Eclipse Integration) to manage the dependencies for my Java projects in Eclipse. The automatic download feature for JAR files from the Maven repositories is a real time saver. Unfortunately, it does not include API documentation and source code.
How can I set up Maven to automatically also get the source and javadoc attachments and register them properly with Eclipse?
I am sure m2eclipse Maven plugin for Eclipse - the other way around - can do that. You can configure it to download both the source files and javadoc automatically for you.
This is achieved by going into Window > Preferences > Maven and checking the "Download Artifact Sources" and "Download Artifact JavaDoc" options.
mvn eclipse:eclipse -DdownloadSources=true
or
mvn eclipse:eclipse -DdownloadJavadocs=true
or you can add both flags, as Spencer K points out.
Additionally, the =true portion is not required, so you can use
mvn eclipse:eclipse -DdownloadSources -DdownloadJavadocs
The other answers on this work, but if you want to avoid having to remember command line arguments, you can also just add to the downloadSources and downloadJavadocs config to the maven-eclipse-plugin section of your pom.xml:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
... other stuff ...
</configuration>
</plugin>
</plugins>
</build>
...
</project>
I prefer not to put source/Javadoc download settings into the project pom.xml file as I feel these are user preferences, not project properties. Instead, I place them in a profile in my settings.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<profiles>
<profile>
<id>sources-and-javadocs</id>
<properties>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>sources-and-javadocs</activeProfile>
</activeProfiles>
</settings>
Right click on project -> maven -> download sources
If the source jars are in the local repository and you are using Eclipses maven support the sources are getting automatically attached. You can run mvn dependency:sources to download all source jars for a given project. Not sure how to do the same with the documentation though.
If you are using meclipse do
window --> maven --> Download Artifact Sources (select check)
(If you still get attach source window, then click on attach file button and close the attach source window. The next time you try to see the source it will open the correct source)
There is also a similiar question that answers this and includes example pom settings.
I tried windows->pref..->Maven But it was not working out. Hence I created a new class path with command mvn eclipse:eclipse -DdownloadSources=true and refreshed the workspace once. voila.. Sources were attached.
Source jar's entry is available in class path. Hence new build solved the problem...
in my version of Eclipse helios with m2Eclipse there is no
window --> maven --> Download Artifact Sources (select check)
Under window is only "new window", "new editor" "open perspective" etc.
If you right click on your project, then chose maven--> download sources
Nothing happens. no sources get downloaded, no pom files get updated, no window pops up asking which sources.
Doing mvn xxx outside of eclipse is dangerous - some commands dont work with m2ecilpse - I did that once and lost the entire project, had to reinstall eclipse and start from scratch.
Im still looking for a way to get ecilpse and maven to find and use the source of external jars like servlet-api.
Changing pom for maven-eclipse-plugin to include source/javadoc just apply for new dependencies being added to pom. If we need to apply for existing dependencies, we must run mvn dependency:sources. I checked this.
Checking download source/javadoc in Eclipse-Maven preference, sometimes is not enough. In the event maven failed to download them for some reason (a network blackout?), maven creates some *.lastUpdated files, then will never download again. My empirical solution was to delete the artifact directory from .m2/repository, and restart the eclipse workspace with download source/javadoc checked and update projects at startup checked as well.
After the workspace has been restarted, maybe some projects can be marked in error, while eclipse progress is downloading, then any error will be cleared.
Maybe this procedure is not so "scientific", but for me did succeded.
I've added the pom configuration to the maven-eclipse plugin to download source and javadocs, but I figure/hope that will happen for new dependencies, not existing ones.
For existing dependencies, I browsed in package explorer down to the "Maven Dependencies" and right-clicked on commons-lang-2.5.jar, selected Maven | Download Sources and... nothing appeared to happen (no progress bar or indication that it was doing anything). It did, however, download as I'm able to jump to source in commons-lang now.
overthink suggested using the setup in the pom:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
... other stuff ...
</configuration>
</plugin>
</plgins>
</build>
...
First i thought this still won't attach the javadoc and sources (as i tried unsuccessfully with that -DdownloadSources option before).
But surprise - the .classpath file IS getting its sources and javadoc attached when using the POM variant!
For Indigo (and probably Helios) the checkboxes mentioned above are located here:
Window -> Preferences -> Maven
I had a similar problem, and the solution that worked best for me was to include the source in the same jar as the compiled code (so a given directory in the jar would include both Foo.java and Foo.class). Eclipse automatically associates the source with the compiled code, and automatically provides the JavaDoc from the source. Obviously, that's only helpful if you control the artifact.
After Setting the Properties either at Project Level or User Properties level,
Please do a Maven -> Update Project (Force Update). It downloads the sources
A Small addition to the answer, if your project is not a maven project still you can get the source code of the jars, by using this plugin provided in eclipse
Java Source Attacher