Mapping maven structures to Eclipse structures, then analyzing with JDT - eclipse

I'm trying to generate Java unit tests using Eclipse's JDT. Part of that is determining the classpath that pertains to a particular Java class.
I imported a Maven project named jaffa-framework into Eclipse. Jaffa-framework has several Maven modules, including jaffa-api. Each of the submodules has its own pom.xml. I can build jaffa-framework and its submodules with no problem using Maven from within Eclipse (although it will not build using Project -> Build Project).
<project ...
<artifactId>jaffa-framework</artifactId>
...
<profiles>
<profile>
<id>normal</id>
<modules>
<module>jaffa-api</module>
...
</modules>
</profile>
</profiles>
...
</project>
When I import jaffa-framework into Eclipse as a Maven project, I see the following in the UI:
A Java project: jaffa-framework
Two Folders/IPackageFragmentRoots for jaffa-api:
jaffa-api/src/main/java and jaffa-api/src/test/java
One "plain" folder: jaffa-api
This structure looks weird to me. I expected to see something more like:
jaffa-framework
jaffa-api
jaffa-api/src/main/java
jaffa-api/src/test/java
Perhaps I did something wrong while importing? If you have ideas, please let me know.
If I select a Java file within jaffa-api/src/main/java, I can use JDT to navigate to the jaffa-framework project and get its classpath using javaProject.getResolvedClasspath(true); however, what I really need is the classpath for jaffa-api.
The jaffa-api folder contains a pom file, a .project file, and a .classpath file. I don't see an easy way to navigate to it using JDT, and because it is a folder, I don't see how I could obtain the classpath programmatically using JDT.
I'm also wondering why jaffa-api looks like a plain folder, instead of something more project-like. Eclipse must know that it is some kind of subproject, because the folder contains .classpath and .project files.
Please tell me how I can access the classpath information of jaffa-api using JDT, and/or how I can correct my project set up.

There was something screwy about my original project creation. I'm not sure what.
I solved my problem by deleting my jaffa-framework project by right-clicking on it and choosing "Delete" from the menu. I re-imported the project using File -> Open Projects from File System. I'm guessing the key thing to notice here is that the "Search for nested projects" box is checked. After the project was reloaded, both jaffa-framework and jaffa-api appeared as projects.

Related

Eclipse maven externalToolBuilder

I have a maven project, and i'm surprised that .project and .externalToolsBuilder are under svn.
Someone explain to me that its because we need to create a builder automaticaly when developer's import project under eclipse.
Does it possible to add in pom.xml all that i need to create this builder when project is imported into eclipse wihout need to commit .project file?
Because i want to have a clean directory without .project/.classpath etc... before import my project and i don't want this file under svn
thanks a lot.
The answer to your question is clearly "yes". It is possible to set up Eclipse from a pom.xml file and have Eclipse generate the files it needs from there.
Yes, you can generate the .project file, but not the files in the .externalToolBuilders folder. And if you have custom builders those should be added with <additionalBuildcommands>. See eclipse maven plugin.
So, the .project file shouldn't sit in svn, .externalToolBuilders. You can generate those too, as you can see in the eclipse maven plugin, but the content still has to be somewhere (your pom, a server etc).

self-describing project in Eclipse using m2e

I'm using Eclipse and m2e to import a Maven project into Eclipse.
The eclipse documentation states the following about the .project file:
The purpose of this file is to make the project self-describing,
so that a project that is zipped up or released to a server can be
correctly recreated in another workspace.
However, the .project file does not contain no information about the imported Java files nor the pom.xml. It only contains some build commands and natures. Thus, I don't understand how it makes the project self-describing.
It seems that each developer that downloads repository for the first time will need to import the Maven project inside Eclipse again. I'd like to have a meta file that contained at least the pom.xml path so that developers could have a ready environment after downloading the repository content and didn't need to import the Maven project into Eclipse. Is that possible?
Technically speaking, Eclipse doesn't know anything about maven. The .project file is Eclipse's way of storing metadata about the project in a file that's contained within the project (although nowadays some of that metadata has moved to the manifest.MF file). The .project file still contains information about natures (which are used by tools to identify which projects are of interest to the tools) and builders (which describe what tools to invoke as resources in the project are changed). In this way the .project makes the project self describing.
Maven is a relative newcomer (albeit a stable and well-established one) to the Eclipse world. As you point out maven stores all of its project info in a pom.xml file. Maven fits itself into the Eclipse world by providing two actions, "Update Project Configuration" and "Update Project Dependencies", that take the information in the pom.xml file and update the contents of the .project and .classpath files.
So instead of manually editing the .project and .classpath files as you suggest, a developer only has to invoke those two functions after extracting an Eclipse project from a repository.

new maven POM file in eclipse java project

Have upgraded to eclipse.indigo using a new installation. Added m2e. Maven shows up under Windows>Preferences. File>New>Other>Maven gets the following options: Checkout ..., Maven Module, Maven Project. The Maven POM file option is missing. I'm trying to add Maven support to an existing Java project. Have read the manuals, searches, helps, etc. No explanation as to why the Add POM option is missing. Any idea how to get this option to appear?
In order to create a POM on your project, and also change the project structure to Maven default, right-click on your project > Configure > Convert to Maven Project.
Don't forget to choose the correct Packaging type (in your case I guess it will be POM).
I don't have this option either, seems to have disappeared with the latest version. This bug report seems to point in this direction as well:
https://issues.sonatype.org/browse/MNGECLIPSE-2709
The standard way should be to create a new Maven project through the wizard, which will then also contain a POM file. You could use that to create a dummy project and then copy over the POM file.
In most cases, I also just copy an existing POM file to the project and start with that.
Quick workaround: Adding a POM by hand. (Also I'm not sure that I've ever seen an "Add POM" option.)
Simply add a file to your project (New > General > File), and name it pom.xml. On double-click on this file, the POM Editor will open.

Eclipse and Maven configuration

I'm new to Maven. I've just installed and configured it.
When I started coding, I realized Eclipse doesn't show the error messages.
I haven't found anything helpful by searching Google. Some sources say to add java builder to your project but I already did.
Here's a screenshot.
It looks, that you had project in eclipse configured to have source code in 'src' subdirectory (and compile classes to 'bin').
Maven by default looks for java sources in src/main/java (and compile classes to 'target/classes').
So you'd need to move your source code to src/main/java or modify pom.xml to point to 'src' as source directory.
I'd suggest following maven convention and store source code in src/main/java.
You didn't create the Maven standard folder structure.
Create the structure like that:
src/main/java for Java classes and src/main/resources for properties etc., then do a right click on the project and choose Maven -> Update project configuration.

Eclipse doesn't find source attachements

When opening a class included through a Maven dependency, Eclipse tells me that the containing .jar file has got no source attachment.
But in the directory in which the .jar file resides, there is also as corresponding -sources.jar file.
Do you have any hints regarding the solution of this problem?
Is there a way to tell Eclipse where it should automatically look for the source attachment?
Edit: In the Eclipse .classpath file, the sourcepath value is set for external archives, but not for ours. But both jars and sources file reside in the same repo.
The property -Declipse.useProjectReferences=false is also set.
The problem was that there were already sources.jar-not-available files for certain projects in my local repository.
I've deleted them and now the attachment of source files is working. Obviously Eclipse respectively the Maven Eclipse plugin doesn't look for source attachments as long as these marker files exist.
If it hadn't been for the comment from K. Claszen I'd have never came up with this solution.
Check that you have downloadSources in the maven-eclipse-plugin section of your pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
...
</configuration>
</plugin>
If you are using m2eclipse plugin for integrating Maven projects in Eclipse, you might need to enable downloading sources and javadocs in m2eclipse preferences:
Window > Preferences > Maven > Download Artifact Sources
For me the file was in place in local repo and eclipse (4.5) kept complaining there is no associated source jar (sources.jar-not-available did not existed). I ended up removing the directory from local repo, letting eclipse to download the sources again repolulating the repo, running mvn eclipse:eclipse and refreshing the project.
For me all these steps was necessary to get this working.
For those who are still having trouble downloading sources for your Maven dependencies (like me), you can try this alternate method:
Find the particular Maven dependency you wished to inspect Java source code for. You need to have the Maven artifact name.
Next, navigate to the Maven repository server where you download the Maven dependency from. For example, I have spring-security-core-5.5.1.jar as my Maven dependency. The repository host site is from mvnrepository.com, in which the repository server is from https://repo1.maven.org/maven2/.
Navigate to mvnrepository.com and search for your Maven dependency artifact. In my case, it would be spring-security-core, and I located the artifact information page here.
On this page, in the table row labeled Files, you want to click on View All, on the right of the JAR link with how many kilobytes displayed to the side. (Underlined)
Once you're viewing the list of all JARs provided with the Maven dependency, click and download the one that says, [artifact name]-[artifact version]-sources.jar. In my case, it's spring-security-core-5.5.1-sources.jar.
Once downloaded, on your local machine, use your File Explorer to navigate to the folder containing the artifact JAR which you have trouble downloading the source codes for. In my case, it would be $MAVEN_HOME/repository/org/springframework/security/spring-security-core/5.5.1.
Move the [artifact name]-[artifact version]-sources.jar into this folder.
Go back into Eclipse, and try attaching the sources. You should be able to pick up the ...-sources.jar file upon attaching it.
After attaching it, you will then be able to see the source codes for your Maven dependency.
Right click at project, select Maven, select Download Sources.
I download a maven project from online,use mvn eclipse:eclipse converted to eclipse project then I occurred the problem.
solution is remove all external lib that start with 'M2_REPO',it's work for me.