maven - use generics in a eclipse's maven project - eclipse

I have a maven website project generated by archtype plugin with eclipse IDE. The problem is when i need to use generics or any thing of java 5 or 6, i changed the project compiler to 1.6 and i got a error mark on the project icon in the package explorer but everything is fine. I think it is cause by maven but I dont know how to get rid of it.
Thank you

Don't forget to configure the maven-compiler-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<!-- Lock down plugin version for build reproducibility -->
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>

I second with what Mike has said.
Even after changing the entries in pom, i was still getting the compiler error.
and it worked onlt after deleting the project and importing it again in the workspace

You need to make sure Eclipse is compiling with the Java 1.6.
Go to your project-specific settings: Project > Properties. Type "Java Compiler" in the text box to the left.
Ensure that all those drop-downs under "JDK Compliance" are set to 1.6. Otherwise check the box to 'Enable project-specific settings' and manually set them to JDK 1.6.
You may also want to enforce this through Windows > Properties, Java > Compiler, Compiler compliance level > 1.6, for global default settings.
Also make sure that the version of the JDK on your build path is the one you want. Hope this helps.

This is kludgy, but I've run into similar issues and worked around them by deleting the project (and not the sources obviously). Then I would import the maven project into my workspace.
m2eclipse ends up resolving the project again and sets up the project correctly.

Related

Eclipse JDT and Maven/Tycho do not agree on warnings

This is related to How to avoid Eclipse importing a class when putting the class name in the comments, so that checkstyle does not complain later?, but is a different issue.
I have a bunch of Javadoc references with import statements, as describe in the referenced question. Eclipse does not warn about this, but I still get compiler warnings when building my code with Maven/Tycho. I thought that specifying <compilerId>jdt</compilerId> should make Maven use the same compiler as Eclipse does, and there by generate identical sets of warnings.
I understand that I can use the fully qualified name in the Javadoc tag to avoid the import statement, but what I wonder here is how do I get the same set of compiler warnings when building with Maven/Tycho as when I build in Eclipse?
Tycho uses the JDT compiler by default, so you don't need to set the <compilerId>. However the compiler settings defaults may be different in Tycho and in Eclipse, or you may have changed the default settings in your workspace. You could try to configure the compiler in Tycho via the <compilerArgs> parameter to match your workspace settings, but this may get quite tricky.
With Tycho 0.22.0 (cf. bug 404633) there is much easier way to get the exact same compiler settings in Eclipse and in Tycho:
Enable project-specific Java compiler settings in Eclipse and configure them in the way you want them. This creates a file .settings/org.eclipse.jdt.core.prefs in the project.
Add the following Maven configuration:
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-plugin</artifactId>
<version>${tycho-version}</version>
<configuration>
<useProjectSettings>true</useProjectSettings>
</configuration>
</plugin>
This makes the JDT compiler in Tycho use the Eclipse project settings whenever they are present.
Note that you need to put the .settings/org.eclipse.jdt.core.prefs files under version control to make your build reproducible.

Java compiler level does not match the version of the installed Java project facet

I have created a New Dynamic Project under Eclipse Helios Version, where my JRE Version is set to 1.6.
I have added Maven capabilities to the Web Application by clicking on Configure → Convert to Maven Project.
After adding this, a build error appeared in the Eclipse Problems view:
Java compiler level does not match the version of the installed Java project facet.
Unknown Faceted Project Problem (Java Version Mismatch)
Please tell me how to resolve this error (I want to have my JRE version as 1.6 only).
If your project is not a Maven project, right-click on your project and choose Properties to open the Project Properties dialog.
There is a Project Facets item on the left, select it, look for the Java facet on the list, choose which version you want to use for the project and apply.
Assuming that you are using the m2e plugin in Eclipse, you'll need to specify the source and target versions as 1.6 for maven-compiler-plugin. m2e uses these values to determine the project's Java compiler level. A snippet of the POM is shown below:
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
Alternatively, you can specify the maven.compiler.source and maven.compiler.target properties with values of 1.6, that happen to be the equivalent:
<properties>
<maven.compiler.target>1.6</maven.compiler.target>
<maven.compiler.source>1.6</maven.compiler.source>
</properties>
TK Gospodinov answer is correct even for maven projects. Beware: I do use Maven. The pom was correct and still got this issue. I went to "Project Facets" and actually removed the Java selection which was pointing to 1.6 but my project is using 1.7. On the right in the "Runtimes" tab I had to check the jdk1.7 option. Nothing appeared on the left even after I hit "Apply". The issue went away though which is why I still think this answer is important of the specific "Project Facets" related issue. After you hit OK if you come back to "Project Facets" you will notice Java shows up as version 1.7 so you can now select it to make sure the project is "marked" as a Java project. I also needed to right click on the project and select Maven|Update Project.
I found #bigleftie's comment above very helpful:
"Four things must match
Project->Java Build Path->Libraries->JRE version
Project->Java Compiler-> Compiler Compliance Level
Project->Project Facets->Java->Version
(if using Maven) pom.xml - maven-compiler-plugin artefact source and target".
In my case, in the project properties, Java compiler, the JDK compliance was set to use the workspace settings, which were different from the java version for the project. I clicked on 'Configure Workspace Settings', and changed the workspace Compiler compliance level to what I wanted, and the problem was resolved.
I resolved this problem by setting the java version in Project Facet property of the project properties, Right click the project root folder -> Properties, search for Project Facets, and select compatible java version.
For reference -
I changed the configuration inside workspace/project/.setting/org.eclipse.wst.common.project.facet.core to :
installed facet="jst.web" version="2.5"
installed facet="jst.java" version="1.7"
Before changing config, remove project from IDE.
This worked for me.
I resolved it by Myproject--->java Resource---->libraries-->JRE System Libraries[java-1.6] click on this go to its "property" select "Classpath Container" change the Execution Environment to java-1.8(jdk1.8.0-35) (that is latest)
The Project Facet->Java should match whatever you have in the pom.xml for the maven-compiler-plugin artifact source and target.This is perfect.But if you donot have it here then you can also fix it by matching Java compiler version in Porject-Facets from the setting: Eclispe->Preferences->Java->Compiler
You can change project facet from
Project --> Properties --> Project Facet --> Java --> {required JDK version}
Right click the project and select properties
Click the java compiler from the left and change to your required version
Hope this helps
In Eclipse, right click on your project, go to Maven> Update projetc. Wait and the error will disappear. This is already configured correctly the version of Java for this project.
If using eclipse,
Under.settings click on org.eclipse.wst.common.project.facet.core.xml
<?xml version="1.0" encoding="UTF-8"?>
<faceted-project>
<installed facet="java" version="1.7"/>
</faceted-project>
Change the version to the correct version.

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>

What's the "right" way to (temporarily) exclude sources from a maven build, and is there an easy way to do it from Eclipse?

I'm new to maven, and I don't have a whole lot of experience with Eclipse either.
To exclude java files from building in Eclipse, I right click the files and choose Build Path -> Exclude. This works great for the "on-save" compilation in Eclipse, but it does not propagate to the maven project, so when I build my project with mvn install, it tries to compile the excluded sources.
I've done a few searches and the results point me to the compiler plugin and the <excludes> functionality, but editing maven project files in order to temporarily exclude a file from the build seems a bit awkward.
What's the "right" way to (temporarily) exclude sources from a maven build, and is there an easy way to do it from Eclipse, via the m2eclipse plugin or otherwise?
You could use the <excludes> parameter in Maven Compiler plugin to temporarily exclude files from compilation.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<excludes>
<exclude>**/model/*.java</exclude>
</excludes>
</configuration>
</plugin>
If you are using M2Eclipse plugin and run Maven->Update Project Configuration, the excluded files in the pom should automagically get excluded from eclipse compilation as well.
if you choose maven as project management, then you really have to do it the "maven way".
Eclipse builds the project based on the classpath specified in project properties and it doesn't relate to classpath of maven compiler plugin. "mvn compile" is driven only by configuration of compiler plugin.
Usually these "temporary" changes are dealt with by JVM parameters appended to the maven goal (maven plugin/Mojo goal that you are running from cmd), that you create (custom one) and save in "Run as" > "Run configurations". I use commandline (shell) rather than m2eclipse for maven. changing parameters is quicker for me.
To find out what parameters you can use, you can either specify the particular Mojo (maven plugin) in you maven dependencies (just temporarily) and look at its sources right in eclipse, you can see parameters that can be specified via "-D" JVM parameters. Or you can check the documentation.
In compiler plugin there is a parameter private Set<String> excludes = new HashSet<String>(); but unfortunately collection parameters cannot be specified as JVM parameters... So the only option left is configure the plugin declaration in pom.xml.
Then there are also profiles, but they are not useful for this case.
To summarize it, your requirement is rather rare, excluding a java class from compilation is not a usual requirement.
I hope it helps

I have build errors with m2eclipse but not with maven2 on the command line - is my m2eclipse misconfigured?

I installed m2eclipse to build a java project in Eclipse.
I imported the project as follows:
Import->
Maven->
Existing Maven Projects->
Import Maven Projects->
- Select root directory
- Select pom file
- Click Finish
To be sure m2eclipse was actually building the project, I deleted the target directory and made sure it was re-created by m2eclipse and it was. But unlike with the command-line version of maven which built everything perfectly, m2eclipse leaves a large number of build errors in the source code.
Is it possible that I did not configure m2eclipse properly? How would I check this?
This is a github link to the project I'm trying to build. I'm getting the #Override build errors at this line. It says "The method createNewToken must override a superclass method".
Update: The problem is the same as the one described in ‘Must Override a Superclass Method’ Errors after importing a project into Eclipse and here is what the accepted answer says:
Eclipse is defaulting to Java 1.5 and you have classes implementing interface methods (which in Java 1.6 can be annotated with #Override, but in Java 1.5 can only be applied to methods overriding a superclass method).
Changing the compiler level to Java 1.6 would make the problem go away. To do so, modify the compiler plugin configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
And update the project configuration (right-click on the project then Maven V Update Project Configuration) does solve the problem under Eclipse.
Or stick with 1.5 but remove the problematic #Override annotations.
I don't know how Taylor got things working with a Java 1.5 compiler level. And my guess is that the project wouldn't build on the command line with a JDK 5.
But unlike with the command-line version of maven which built everything perfectly, m2eclipse leaves a large number of build errors in the source code.
Hard to say what is happening exactly without seeing those "errors" (are them really errors?). Please provide some traces.
Is it possible that I did not configure m2eclipse properly? How would I check this?
One difference is that m2eclipse uses by default a embedded version of Maven 3 which is probably not the same version that you use on the command line. You can change that through Window V Preferences V Maven V Installation (and add your own installation):
But while I would recommend to use the same version under Eclipse than on the command line, this is very likely not the root cause of the problem, Maven 2 builds should run on Maven 3 without problems.
I checked out the code. I have exactly the same problem. The code seems to be just broken.
Edit: It definitely is. Look at the class com.jappstart.service.auth.UserDetailsServiceImpl. It wants to override the method public final UserDetails loadUserByUsername(final String username) but this method doesn't exist in the interface the class implements and is has no superclass.
Edit: Ok, that doesn't explain why it builds with maven standalone. This also works for me. Very strange. It seems that there is something going on with the build that doesn't work with m2eclipse.
Edit: I'm pretty sure the code works because the bytecode is modified by the datanucleus plugin. When I run the project as maven build (right-click->Run->maven package) it sucessfully creates the war with m2eclipse. So my guess is that the problem is with the m2eclipse Maven Builder.
The override errors will appear if eclipse is configured to use java 1.5 instead of 1.6. Check the project properties.