I am trying to add java code from a Maven project (called docx4java) which I checked out from svn to an existing Eclipse project (called DocumentManager). I have tried the normal way, that I thought would work, i.e.:
Right Click on eclipse project>Properties>Java Build Paths> Projects > Add (here I add the Maven project) and >Libraries (here I specify Native Library location e.g. docx4/trunk/docx4/src/) but I still can't get the 'Maven' classes to be recognised in eclipse. I get the message
import docx4j.src.main.java.org.docx4j.convert.out.flatOpcXml.FlatOpcXmlCreator cannot be resolved
I have tried adding a test project that was compiled in Eclipse and that works fine, Eclipse seems to recognise it i.e. import org.me.TestProject works fine.
How can I get the code from the Maven project docx4java to work in the Eclipse compiled project DocumentManager?
You can generate eclipse poject files (which you can just import into your Workspace) using the maven eclipse plugin
Take a look at these ecplise plugins for a more direct integration of maven into eclipse
You should create a eclipse project for docx4java as #Attila has commented. Once that is done, you should have two projects on your workspace: docx4java and DocumentManager. At this point, what you are doing now (adding a project reference to the Java Build Paths) should work.
It seems that the source path is not configured correctly, this is why the compiler cannot find your "Maven classes".
docx4java.src.main.java...
Maven has a different directory structure and you have to tell Eclipse where to find your sources.
For a "vanilla" eclipse project, remove the standard eclipse build path src and add the maven source path src/main/java (or whereever you added the code), the compiler should correctly pick up your sources.
Note that you still have to add all jars mentioned in the dependencies section of the maven pom.xml file. Otherwise it won't compile, even if it finds your added java classes.
(Addendum: I would recommend to use maven in your existing project as well.)
Related
I have two gradle java projects imported into Eclipse, one being a dependency of the other.
I would like for Eclipse to use the local dependency code, instead of the compiled dependency in the gradle cache folders, so I can modify and debug both projects simultaneously.
How do I force Eclipse to use a local dependency code?
The feature you are referring to is known as Composite Builds:
Importing into the IDE
One of the most useful features of composite builds is IDE integration. By applying the idea or eclipse plugin to your build, it is possible to generate a single IDEA or Eclipse project that permits all builds in the composite to be developed together.
In addition to these Gradle plugins, recent versions of IntelliJ IDEA and Eclipse Buildship support direct import of a composite build.
Importing a composite build permits sources from separate Gradle builds to be easily developed together. For every included build, each sub-project is included as an IDEA Module or Eclipse Project. Source dependencies are configured, providing cross-build navigation and refactoring.
The most simple way of achieving this is to use includeBuild in your settings.gradle.
rootProject.name = 'my-composite'
includeBuild 'my-app'
includeBuild 'my-utils'
With that in place, there's no need to configure the build path manually in Eclipse.
Gosh this was a trivial one and it took me way too long to figure it out.
Solved the issue by creating a file named external-projects.properties in the root folder of the parent project with the relative or full path to the dependency project folder.
To be precise, for me the folders tree ends up like this:
git-folder/
|__parent-project/
|__external-projects.properties
|__build.gradle.kts
|__rest (src/main, etc)
|__dependency-project/
|__build.gradle.kts
|__rest (src/main, etc)
And the contents of the external-projects.properties file:
dependency-project = ../dependency-project
The dependency project must also be in the build path of the parent project (done by "Right-Click project > Build Path > Configure Build Path" and adding it in the "Projects" tab).
After that, a "Right Click > Gradle > Refresh Gradle Project" did the trick.
I have a fundamental question, but something which bothers me a lot. We sync code from perforce in my company. and then we are taught to build the code compatible to eclipse. Then we import the code in eclipse as existing project. Then when we hit ctrl-space from an object, we get suggestions of methods.
My question is: Why do we need to build the code for suggestions of methods? . After syncing from perforce, what I have is still an existing java project (right?). So eclipse should be smart enough to be able to index and find out from source code, the methods, when I hit ctrl-space on an object?.
Why do I need to build? What purpose the build serves?
The code does not have to be built, but to be able to import a directory as project via File > Import...: General > Existing Projects into Workspace into Eclipse, at least the file .project must exist.
There are several ways to import, for example, a Maven project:
Execute mvn eclipse:eclipse on the command line and in Eclipse do File > Import...: General > Existing Projects into Workspace
File > Import...: Maven > Existing Maven Projects without a command line call uses by default the in Eclipse embedded Maven
File > Open Projects from File System... (or in Git Repositories view right-click + Import Projects...) detects not only Maven projects but also plain Java projects and automatically configures them accordingly.
In all three ways the following files are created, which are required in Eclipse for the Java content assist (Ctrl+Space) to work:
.project - says whether it is e. g. a Java, a PHP or a C/C++ project (to be more precise, the project name, builders and natures are specified here, e. g. the project folder icon of a Java Maven project will be decorated with a M and a J based on the Java and Maven project natures)
.classpath - tells Eclipse where the source and output folders are and which JARs should be added to the classpath (in Maven projects, the JARs are not listed, but it refers to the Maven dependencies which are computed from the pom.xml file)
.settings/org.eclipse.jdt.core.prefs - contains Java compiler and optional formatter settings (e. g. which Java version; which problems should be ignored or shown as infos, as warnings or as errors, etc.)
With Eclipse Oomph you can automate even more: you select a project and based on a project-specific configuration, for example, a Git repository is automatically cloned and plug-ins necessary to edit the project are automatically installed and configured.
Eclipse uses relfection for the suggestion, it can be possible only if the .class file generated
In an eclipse project, I referenced a class that was currently not in project's build path.
In the context sensitive pop-up that opens by hovering the mouse over the error, "Fix project setup" option has offered me to add a jar to the build path of the project. This jar is in my local maven repository.
However, the "correct" way to fix it is obviously by adding the relevant dependencies to my project's pom, instead of adding this jar to the build path of the project.
So is there a way to configure eclipse, so that it won't offer to add a maven repo jar to the build path, but it will offer to fix the project's pom?
This feature used to exist in m2eclipse (the predecessor of m2e), see e.g.
https://blog.sonatype.com/2010/03/adding-dependencies-using-m2eclipse/ which shows a "Search dependency for XXX" quickfix above the "Fix project setup..." quickfix.
Unfortunately it didn't make it into m2e, see https://www.eclipse.org/lists/m2e-users/msg01130.html
Make sure:
your project is fine outside of eclipse using only maven/pom, mvn
clean install (or similar) in an external shell/console
your eclipse project has maven nature
Eclipse -> Project -> Build Automatically is checked
Refresh your eclipse project
Do something in the pom.xml (save file) should trigger a build
I've been coding Java in Eclipse for awhile without needing to specify dependencies. Now that I learned how Maven does it, I'm wondering: how did Eclipse build projects on its own? How did Eclipse figure out which versions of imports, and which dependencies of dependencies, are needed to make everything work?
Finally, what are the advantages and disadvantages of building a project in Eclipse by starting with New->Other->Maven Project instead of New->Java Project?
To the first question: Eclipse doesn't add any dependency in a standard Java project. You have to manually add to the Build Path all needed JARs, otherwise you'll have compilation errors.
To the second question: if you create a standard Java project Maven is not used, even if you create a pom.xml file in the root of the project. You can always convert a standard Java project in a Maven project (see Convert Existing Eclipse Project to Maven Project).
Your dependencies has always to be in the classpath
When you start a project with Maven, Eclipse will automatically add the Maven repository to the classpath.
When you start a Java project you have to link your library manually in Eclipse and the version of the library is the one you've downloaded.
You can see the difference in your project's Properties > Java Build Path > Libraries
I have downloaded a 3rd party project, which consists of multiple files, including Java ones.
General structure is as follows:
<topfolder>
pom.xml
<subfolder1>
pom.xml
src
main
java
<normalclasspath>
resources
site
apt
index.apt
test
java
<normalclasspath>
Eclipse imports this project normally, but is unable to index its Java content. For example, I can't browse from a variable to its definition, and so on.
Also I can't set my own Build Path since it says No action available.
Of course, I can refactor folder structure myself to suite eclipse needs, but are there any automation means for this?
UPDATE
Yes, this is a Maven project and Eclipse already knows that it has Maven nature. The only option now is to disable it
But this is not a question. The question is how to add JAVA NATURE, so that Eclipse knows consistence of classpath and be able to navigate to class definitions and so on.
UPDATE 2
#75inchpianist's answer about facets helped partially. It was not available to select Java facet immediately, but required turning on facets at all first. Then Java facet was already there.
Now I see, that Eclipse interprets Java, but Maven interpretation is not full. Namely, no Maven dependencies interpreted (no Maven Dependencies node in Package Explorer).
The attempt to add it fails:
Right click on the project and select "Properties"
Within "Project Facets" make sure that Java is selected!
As this is a maven project (because of the pom.xml) you need to install a maven plugin for eclipse.
Next you have to right click on the project and choose "Configure->Convert to Maven Project".
Now you should have your normal java structure.
Assuming that Eclipse recognizes the project as Java project yu can do the following:
If you don't want to install a maven plugin into Eclipse you can make Eclipse recognize the source correctly by adding every src/main/java folder as Source-Folder to your Build-Path.
This is better than modifying the folder structure as you can update the sources without problem later.
Add following to your .project file and refresh the project.
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>