Why can't I add a "Groovy Nature" to a java project? - java

I have some java projects that have been around for a long time and am currently using a pretty recent version of SpringSource Toolsuite and the groovy plugin which works fine (this was added for people who don't read beyond the first paragraph).
I can create a Groovy project or I can convert a Java project to a groovy project, but I'd really like to add a Groovy nature to my java project (I don't think the team would be very happy about me converting all the projects over to groovy projects!)
The problem is that the "Groovy" menu item containing "Add Groovy Nature" doesn't appear in any java projects (legacy projects or new ones I create). It appears in a Groovy project with "Remove Groovy Nature", but not in java projects.
Everything else works pretty well.
Oh also, not using maven, just straight eclipse & ant--I can deal with the Ant part it's just the eclipse integration that's being probleematic.
Am I missing a setting or something?

My colleague and I had the same problem today. We manually fixed it using basically this process:
Clean all projects without starting a new build.
Exit eclipse.
Add/edit the project configuration files below.
Start eclipse.
Refresh project.
Enable Project->Build Automatically.
Here are the files to add/edit:
.project : Add this line to the <natures> element:
<nature>org.eclipse.jdt.groovy.core.groovyNature</nature>
.classpath : Add this line:
<classpathentry exported="true" kind="con" path="GROOVY_SUPPORT"/>
.settings/org.eclipse.jdt.groovy.core.prefs : New file:
eclipse.preferences.version=1
groovy.compiler.level=-1
Note that the target project was already a java project. To add this stuff to a plain or other non-java project, you might also need to add the following:
.project :
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
.classpath : Copy from one of your java projects and edit appropriately. Here's a default .classpath file:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>

Install Groovy eclipse plugin
http://groovy.codehaus.org/Eclipse+Plugin

Related

Eclipse error: "Could not read maven project" for non-Maven project

I have a git Eclipse project that used to use Maven. I created a new Eclipse project on one computer, imported the source code, and successfully built it. I then committed and pushed my changes to GitHub.
When I cloned the project on another computer (OS X) and imported it, I get this error when I try building:
Could not read maven project
When I click for additional details, I see:
Could not read maven project
Some problems were encountered while processing the POMs:
[FATAL] Non-readable POM /Users/spertus/src/wordui/pom.xml: /Users/spertus/src/wordui/pom.xml (No such file or directory) #
Of course there is no pom.xml file. I am no longer using Maven.
When I open the project Properties, the only item listed under Project Natures is Java.
Here is the .project file:
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>wordui</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
I have tried reopening Eclipse, cleaning the project, and deleting and reimporting it, to no effect.
I don't think .classpath should matter, but here it is:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.launching.macosx.MacOSXType/javafx-jre17">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/Wordnik API"/>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/JavaFX 17"/>
<classpathentry kind="output" path="bin"/>
</classpath>
I am using Eclipse for Java Developers 2022-03. The run configuration is under Java Application.
I was not able to fix the problem, but I was able to work around it by:
Creating a brand new Java project.
Importing the old source code.
Adding the libraries.
Running with the default run configuration.
In other words, if you get to this state, create a new project.

Gradle integration for eclipse keeps on changing .classpath file

My current configuration includes a Java WAR project using gradle with the java and eclipse-wtp plugins, and the latest version of Buildship for Eclipse integration (1.0.3 till this morning and 1.0.4 right now) along with Eclipse Luna.
After following this solution https://stackoverflow.com/a/9820317/1544713 (successfully) to avoid deploying the test classes to my local server, I noticed that every time I refreshed the project or closed and opened Eclipse, the .classpath file and .settings/org.eclipse.wst.common.component were changed to their previous state, which is something I that gives me problems (deploying the test classes to the local server implies that they will fail to be loaded due to the lack of some test time dependencies, and of course an undesired behaviour).
The content of .classpath is changed from (the right one):
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="FROM_GRADLE_MODEL" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="FROM_GRADLE_MODEL" value="true"/>
</attributes>
</classpathentry>
to this undesired state:
<classpathentry kind="src" path="src/test/java">
<attributes>
<attribute name="FROM_GRADLE_MODEL" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src/test/resources">
<attributes>
<attribute name="FROM_GRADLE_MODEL" value="true"/>
</attributes>
</classpathentry>
And in the case of .settings/org.eclipse.wst.common.component, these two lines are added (which again, I don't want them to be present):
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/java"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src/test/resources"/>
I can't figure out if it is Gradle that is changing the files or Buildship, or even Eclipse. In any case, I guess there is a way to stop this happening. I have tried many alternatives, like the following configs in build.gradle:
eclipse.classpath.file.whenMerged { cp ->
cp.entries.findAll { it.kind = "src" && it.path.startsWith("src/test/") }*.output = "target/test-classes"
}
eclipse {
wtp.component {
file.withXml { xml ->
def node = xml.asNode()
def wbrNodes = node.'**'.findAll { it.name() == 'wb-resource' && it.'#source-path'.startsWith("/src/test/")}
if (wbrNodes.size() > 0) {
wbrNodes.each { n -> n.parent().remove(n) }
}
}
}
}
But this configuration works erratically (sometimes it seems to work, some other times it doesn't, and actually the first piece of code that starts with eclipse.classpath.file.whenMerged never works).
Thanks in advance.
After some time spent looking for possible solutions, and the cause of the problem, I've found out that the real problem was in fact Buildship, which was not paying any attention the the directives to the eclipse-wtp plugin in build.gradle, and instead it was taking its own approach to generate a .classpath and the related eclipse configuration files. At the same time, and so far (Buildship 1.0.5 released just today) there is no way to configure or manipulate Buildship while it is building its own model (when importing a Gradle project, when opening Eclipse or when refreshing a project e.g. with F5). As Lance_Java said in the Gradle forums (https://discuss.gradle.org/t/gradle-integration-for-eclipse-keeps-on-changing-classpath-file/11813/7?u=fbudassi), it's useless to use the eclipse-wtp plugin together with Buildship, since both take their own approach to generate the Eclipse configuration files.
So, the solution so far has been to remove Buildship from my Eclipse installation and instead replace it with the Gradle IDE Pack 3.6.x+0.17 plugin right from the Eclipse marketplace, which uses the build.gradle directives to build its own model, avoiding this way any possible conflict.
If anybody from Buildship gets to read this post, please, give us some hooks to the generation of the files. Again, as Lance_Java suggested, something like this would really help:
apply plugin: 'buildship'
buildship.wtp.component.file.withXml { ... }

Make maven add classpath entries to eclipse project

Maven can generate eclipse project files by running "mvn eclipse:eclipse"
When I run some class from Eclipse IDE, it sets classpath entries to
target/test-classes
target/classes
*many paths to maven artifacts used in project"
However, I'd like to write additional classpath entries in .project
How to make maven to do that?
I use maven 3.1.1
add those additional required entries as dependencies in pom.xml, if those are directories then just edit .classpath file
then don't use eclipse:eclipse and use
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
in .classpath and add entry for your directory
There are two steps:
Add another folder as sourcefolder to maven
add correct includes to the maven-eclipse-plugin
There is a ppost about configuring eclipse with maven for a scala project, but this should cover exactly your needs. You will simply have to edit the source folders and the include pattern:
https://www.assembla.com/spaces/scala-ide/wiki/With_Maven_CLI
** update **
or use the default folder for this resources src/main/resources

Create a new Java project with standard folders

I am fairly new to the Java build process. Whenever I want to create a new project, I currently create a pom.xml file and the following directories:
src/main/java
src/test/java
target/classes
Then creating a .classpath file with the following:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java" />
<classpathentry kind="src" output="target/test-classes" path="src/test/java" />
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7" />
<classpathentry kind="output" path="target/classes" />
</classpath>
Is there a quicker/easier way to do this? I am using Eclipse as my IDE (if it matters)
You can automate the process with a maven archetype :
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
The quickstart archetype will create a basic maven structure for your project.
Source: Maven in 5 minuts
For the eclipse aspect, it exists a maven plugin to generate the eclipse configuration :
mvn eclipse:eclipse
(Maven eclipse plugin)
To integrate maven with eclipse you can use the maven integration plugin for eclipse(m2e-wtp). The plugin is bundled with the eclipse developer version or available from the eclipse market place.
you don't need to create the classpath manually. Just install m2e-wtp eclipse plugin (or get the eclipse for JEE developers version) and it will manage eclipse classpath for you according to maven configuration.
You can also use maven archetype to generate the base skeleton (pom and directories) for a maven project according to your technology stack.
You could use the Quickstart Maven archetype
As others pointed, you do not need to create pom.xml or .classpath by hand. IDE will do this for you.
You can do this easily by download and installing Eclipse EE . Then you just simply create
File -> New -> Other -> Maven project
This will create you a new Maven project with all directories set up for you.

Is there any way to prevent m2e from modifying the eclipse .classpath file?

I'm converting some of our projects into Maven projects, but m2e changes my .classpath JRE entry from:
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
to:
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
This causes a few errors like:
Description Resource Path Location Type Access restriction: The type
WindowsPopupMenuSeparatorUI is not accessible due to restriction on
required library C:\Program
Files\Java\jdk1.6.0_20\jre\lib\rt.jar DottedJPopupMenuSeparator.java /acommons/src/com/ks/acommons/gui/lookandfeel line
10 Java Problem
Is there any way I can make m2e not generate the .classpath file, or force it to use the workspace default JDK?
In my opinion, Maven is helping you here. It forces Eclipse to use a strict Java 1.6 environment and prevents you from using libraries which are not part of the standard distribution.
Typically this error message is hinting that you should be declaring a new Maven dependency, rather than relying on a JAR file being present in (e.g.) JRE/lib/ext.
This is currently not possible, but it may be if m2e bug 405661 is fixed.
As of today (m2e 1.5.0.20140606-0033), you can only configure m2e to preserve additional classpath entries, but it will always clobber the ones that it automatically generates. This includes the classpath entry for the JRE, so there is no way to set this entry manually and have it survive an m2e project update.
You need to configure Maven to use the correct JRE.
What you're doing is you configured Eclipse to use a certain JRE and now you expect Maven to use the same. This doesn't work because Maven will use the POM (pom.xml) to determine which JRE to use. That makes sense since Maven is a command line tool which runs outside of Eclipse. m2e just configures Eclipse to behave similarly as Maven from the command line. As you can see, Maven is in control here, Eclipse is not!
To make the two work with each other, you need to configure the Maven compiler plugin like so:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source> <!-- JRE version that you need -->
<target>1.8</target>
</configuration>
</plugin>
That will tell Maven which JRE version you want. Now you can configure a matching JRE version in Eclipse preferences and m2e will configure the Eclipse compiler to use the one which has the correct version.

Categories