Make maven add classpath entries to eclipse project - java

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

Related

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

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

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.

Eclipse and IvyDE warning: Classpath Dependency Validator Message

I created a new Dynamic Web Project in Eclipse Kepler, and as it's my first time using IvyDE, I put some dependencies to test it out. here is my ivy.xml:
<ivy-module version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd">
<info
organisation="net.myorg"
module="mymodule"
status="integration">
</info>
<dependencies>
<dependency org="org.hibernate" name="hibernate-entitymanager" rev="4.2.3.Final"></dependency>
<dependency name="log4j" rev="1.2.17" org="log4j"></dependency>
</dependencies>
</ivy-module>
The dependencies where resolved and I can see them in the project libraries.
But after this, my project started giving this warning:
Description Resource Path Location Type
Classpath entry org.apache.ivyde.eclipse.cpcontainer.IVYDE_CONTAINER/?project=myproject&ivyXmlPath=ivy.xml&confs=*&acceptedTypes=jar%2Cbundle%2Cejb%2Cmaven-plugin&alphaOrder=false&resolveInWorkspace=true&retrievedClasspath=true&retrievedClasspathPattern=lib%2F%5Btype%5Ds%2F%5Bartifact%5D-%5Brevision%5D.%5Bext%5D&retrievedClasspathSync=true&retrievedClasspathTypes=* will not be exported or published. Runtime ClassNotFoundExceptions may result. myproject P/myproject Classpath Dependency Validator Message
So if I am understanding it right, Eclipse is warning me that the IvyDE entry in the classpath don't correspond to a resource that will be in the builded project. I suppose this is the expected behavior, if so, is there a way to remove this warning from the project, without disabling every Classpath Dependency Validator Message?
The reason for the warning message is that the Eclipse flexible project deployment mechanism doesn't know whether or not to deploy the libraries referenced by the Ivy container. So you need to explicitly tell it which you want to do, deploy the jars to WEB-INF/lib or not.
If you need this container deployed to WEB-INF/lib you need to:
Right click project go to properties
Select Deployment Assembly
Click Add
Select "Java build path entries"
Select the Ivy container
Click Finish
Make sure the Deploy Path is set to "WEB-INF/lib"
If you look into the .classpath settings file you will see this adds an entry like this:
<classpathentry kind="con" path="org.apache.ivyde.eclipse.cpcontainer.IVYDE_CONTAINER/?project=ivy-test-portlet&ivyXmlPath=ivy.xml&confs=*&ivySettingsPath=%24%7Bliferay_sdk_dir%3Aivy-test-portlet%7D%2Fivy-settings.xml&loadSettingsOnDemand=false&ivyUserDir=%24%7Bliferay_sdk_dir%3Aivy-test-portlet%7D%2F.ivy&propertyFiles=">
<attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes>
</classpathentry>
Notice the <attributes>...</attributes> element has been added. Also my Ivy container path will be different than yours as this is a Ivy container path for one of my own projects.
Now if you don't need to deploy these libraries contained in the Ivy container to your runtime, then you need to tell Eclipse that there is no dependency on these libraries at runtime. You can do that by adding the appropriate attributes in the .classpath file.
Open .classpath file
Find the entry for the Ivy container
Manually add the following attributes element as a child element of the classpathentry
The final element will look something like this. Your path will have different options but the key part is the attributes child element:
<classpathentry kind="con" path="org.apache.ivyde.eclipse.cpcontainer.IVYDE_CONTAINER/?project=ivy-test-portlet&ivyXmlPath=ivy.xml&confs=*&ivySettingsPath=%24%7Bliferay_sdk_dir%3Aivy-test-portlet%7D%2Fivy-settings.xml&loadSettingsOnDemand=false&ivyUserDir=%24%7Bliferay_sdk_dir%3Aivy-test-portlet%7D%2F.ivy&propertyFiles=">
<attributes>
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
</attributes>
</classpathentry>
Either one that you choose, you have explicitly told Eclipse how to handle this library container dependency during deployment. So now you can re-validate the project (right click project and choose "Validate") and you should see the error go away.

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.

Reading a file from a jar, or anywhere on the classpath?

I'm trying to build an application that builds a resource file into a jar, but I'd like to have the project runnable within eclipse. I have a basic maven 2 structure for my project, and I'm unsure how to read in the file such that it's found and used when run from the JAR or from within eclipse. Thought?
Structure:
src/main/java
src/main/resources/file.txt
Current reading method:
getClass().getResourceAsStream("/file.txt")
Is there reading method that will pick up src/main/resources/*, as well as the root level of the JAR (where resources are deployed)?
I don't understand your problem. Resources from src/main/resources are automatically copied over to target/classes and are thus available on the classpath under Maven and Eclipse relatively to the root level at the same location (unless your Eclipse project is not properly configured).
And when packaged inside a JAR, the content of target/classes is packaged "as is" so nothing is changed.
In other words, accessing your file.txt like this is perfectly fine (and this is actually how things are documented):
// Retrieve resource
InputStream is = getClass().getResourceAsStream( "/file.txt" );
// Do something with the resource
...
If you have a problem somewhere, please clarify.
Update: I did a quick test with the maven-eclipse-plugin and I can't reproduce your problem:
$ mvn archetype:generate -DgroupId=com.stackoverflow -DartifactId=q2467362 -Dversion=1.0-SNAPSHOT
...
$ cd q2467362
$ mkdir -p src/main/resources
$ mvn eclipse:eclipse
...
$ cat .classpath
<classpath>
<classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/>
<classpathentry kind="src" path="src/main/java" including="**/*.java"/>
<classpathentry kind="src" path="src/main/resources" excluding="**/*.java"/>
<classpathentry kind="output" path="target/classes"/>
<classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar" sourcepath="M2_REPO/junit/junit/3.8.1/junit-3.8.1-sources.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
</classpath>
The directory src/main/resources is added as source folder as expected. Can you show your POM (especially the resources element if you define one)?
Anything that is put in src/main/resources using maven2 will be placed in the root level of the jar. So the method you are currently using will satisfy both conditions.
From the Maven page:
http://maven.apache.org/guides/getting-started/index.html#How_do_I_add_resources_to_my_JAR

Categories