Including other projects in the Java Build Path using Eclipse and Maven - java

I have been using Eclipse and Maven quite happily (in a largely non-understanding way) for some time to build Java/Selenium/TestNG projects.
I have one project representing a set of utilities that I use in all of the other website-based projects.
To include the utility classes I have simply added the utilities Project to the Classpath via the Java Build Path dialog. This worked well and any changes made to the utilities project were immediately reflected in the projects that used it.
However, when adding Logging capability (slf4j + logback) I started hitting issues with multiple versions being included. I eventually tracked down the issues but in order to solve them I ran "clean install" on both the utilities project and the projects using the utilities.
As a result I am now unable to build the website-based projects without adding the utilities jar as a dependency in pom.xml.
The previous method of simply adding as a Project is still sufficient to remove the errors from displaying on the source files, but now Maven (maven-compiler-plugin:3.7.0) reports a Compilation Failure as it says the utilities packages do not exist.
Looking at the impact of adding a Project to the build path, I see that a line is added to the project's .classpath file:
<classpathentry combineaccessrules="false" kind="src" path="/myutils"/>
but Maven debug shows no indication that this has had any impact on the classpath that it uses to compile.
What did I break when I did "clean install" and how do I restore it?
What mechanism does the Java Build Path Projects tab use to include content (source files or compiled classes?) from other projects in the Workspace?
Alternatively is this just a feature of Maven, that it wants to control everything (but why did it work before?) and is the dependency solution just as good? When including via dependencies (in pom.xml) are changes made to utilities immediately reflected without the need for a complete rebuild?

Related

What is org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER and how do I make it work in IntelliJ?

I have a Java codebase that was written with Eclipse that I am trying to work with in IntelliJ.
The codebase consists of about 20 Java packages built alongside each other as separate projects. (There are about twenty directories, each containing a .classpath and .project file.) They have dependencies on each other. Presumably everything builds just fine in Eclipse.
I had IntelliJ import these from the Eclipse build format. It mostly seemed to work except that Project Settings -> Modules -> Dependencies shows that many projects have an unresolved dependency on org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER. This appears to come from the following entries in the .classpath files:
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
</attributes>
</classpathentry>
The packages with this dependency do not build in IntelliJ because they can't resolve the dependencies on the other packages.
I suspect that org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER is some Eclipse-specific trickery that doesn't translate to a different IDE, but I can't figure out exactly what is going on here.
What is org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER? Do I have to do anything special to a build environment that uses it to make it work outside Eclipse?
IntelliJ Ultimate 2016.1.2, Java 1.8.0_91, OS X 10.11.5
I worked around this problem by going into Project Settings->Modules and manually adding module dependencies between my various projects until everything built. Tedious but effective.
I'm leaving this question up here, though, because a short definition of org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER would still have been helpful to me.
Intro
The IntelliJ IDEA Maven-2 plugin integrates Maven-2 into IntelliJ IDEA.
First,
Second,
Third,
Similar to the Ant plugin, the Maven-2 plugin allows execution of goals (or phases) and redirects output (with error highlighting) to the message window.
Installation
Available for IDEA 6.0 (until version 1.1 also for IDEA 5.0) through IDEAs built-in plugin manager or directly from
{{http://plugins.intellij.net/plugin/?id=1166}}.
In case you downloaded the plugin, simply extract the content of the binary archive into your IDEA plugin directory.
The extracted folder (idea-maven-plugin) contains another folder lib, where all the JARs reside.
Usage
Assuming you have specified the environment variables JAVA_HOME and M2_HOME the plugin should be ready to run without any further configuration.
In case JAVA_HOME is not defined, the plugin uses the internal IDEA JDK.
If M2_HOME is not defined, you must specify the home directory of Maven-2 in the configuration dialogs. This is a change starting from version 1.1 of the plugin.
Before version 1.1, Maven was called via the start script provided with the normal Maven installation. In order to get better access to the running Maven process Maven now is called directly by starting a Java process. This for example allows terminating the running Maven process by the user.
The configuration can be done in two ways:
Either via the IDE Settings or the Project Settings.
The project settings always override the application settings!
The plugin will not work with IDEA versions prior to 6.0!
The plugin was tested under Windows and Linux. It was not tested under Mac OS X.
Resource Link:
Maven 2 Integration
Import Maven dependencies in IntelliJ IDEA
I've come up with a solution that works for me, but the bounty is still open (because I can't close it :P ) and will go to someone who can provide a more detailed explanation.
The solution: Run mvn eclipse:clean and mvn eclipse:eclipse. This causes maven to clear the .classpath file and rebuild it from the pom.xml. Somewhat mysteriously, it no longer contained the dreaded reference to org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER.
Perhaps this is a deprecated tag/variable in Eclipse.

Remove exclusion of resources folder from eclipse build?

I imported my existing maven projects under eclipse.Once imported i found under Java Build Path > Source resource foulder is
excluded(i can see Excluded:**) from build and does not get copied under target > classes directory.
I can remove exclusion manually by selecting Excluded:** and then click Remove, But there are large numbers of projects. Is there
a setting in eclipse where i can do it one go ?
That's part of what Maven does when you let it generate your Eclipse project settings (via the eclipse:eclipse plugin/goal). It's quite misleading. However, if you have the m2e plugins installed into Eclipse it coordinates things correctly.
What's going on, as best as I can figure: Eclipse's built-in Java builder automatically copies non-Java files found on the build path to the output location (unless they're excluded as you see in your Build Path). Problem is, when Maven comes in the picture it, too, wants to copy resources during a build. So to keep the two builders from competing/interfering with each other, Maven instructs Eclipse to ignore resources so it (Maven) can manage copying them during a build.
m2e integrates into the Eclipse build process to do the Maven copying of those files, so if you configure the project as an m2e project things will "just work," both in Eclipse and if you build from a command line.
I don't know of a way to tell Maven to not configure the project this way. To be honest, that's just one of the reasons I prefer to not let Maven generate my Eclipse project files; I do a much better job of it and I totally control the specifics rather than relying on whatever the Maven eclipse developers think is best.
This is an old question, but I've been looking for the answer and I finally dug it up myself. According to the M2Eclipse FAQ the import intentionally excludes resources. This is because Maven performs special resource handling that includes filtering.
Let Maven do the building for you. Disable automatic building in Eclipse and never build your projects in Eclipse.

IntelliJ project where each module has it's own git repository

I am a new IntelliJ user (I've used Eclipse for many years). I'm having a hard time wrapping my head around the-project-is-a-module concept in IntelliJ, since this does not hold true in Eclipse. The main issue I'm having is that I am using my top level package as the project in IntelliJ. I would like this top level package to be in a git repo. I would also like all the dependencies of this package to be in their own respetive git repos. When I check these packages out into my project, a do git status on the top level package, all of the dependencies show up in the untracked files. This behavior seems incorrect to me. How can I fix it?
Thanks!
Edit:
To summarize the clarifications in the comments:
I would like to support hundreds of libraries any of which could change at a time. The dependency graph will also be frequently changing. For this reason, having one git repo or constantly updating .gitignore files is not maintainable.
Currently, I'm using Maven to manage dependencies but I'm open to using whatever is best suited for this job.
Finally, I would like to check out any library into my workspace and modify it and, if possible, have Intellij reflect my local changes when running code as if my local code were already built into the dependency graph. A type of local override if you will.
IntelliJ's directory structure places all of the modules in their parent project's directory. If you are developing libraries which are shared between several other projects, importing the library as a module is probably not the correct solution. Instead, you should treat each library as its own independent project and make "releases" using a build tool such as gradle or maven. Then your projects can treat the libraries the same way they do third-party libraries and use the build tool to import the library.

Applying Maven to a project

I've been asked to apply Maven to a project. After browsing a dozen sites it appears that it's quite vast and I'm not familiar as I'd like with similar tools like Ant. Why is it used/preferred and what does it offer over a standard Eclipse project? Also, how could it be added to an existing project?
Why is it used/preferred and what does
it offer over a standard Eclipse
project?
It is a build tool which can build your project without the need for an IDE like Eclipse. It can create a jar or war or other artifacts from the source, performing a bunch of steps like compilation, running unit tests, etc.
Where maven scores over ant is in managing third-party dependencies and in convention over configuration (which mean less lines of build script if you follow convention).
Also, how could it be added to an
existing project?
You start by creating a new maven project, following the step here.
Place it in the root folder of your project
If your source and resource files do not follow maven folder convention, update maven properties suitably referring to this documentation.
Run mvn package
It will fail if it needs any third party dependencies, which you can add as specified in the doc
With some trial and error, you should have your project running with maven, possibly, much quicker than if you were to set up the same with ant.
Others are already provided sufficient resources to read more about maven.
I suggest to start reading here:
http://www.sonatype.com/books/mvnref-book/reference/public-book.html
Maven is a great tool when you know how to use it. Maven (at core) is a dependency manager.
You include in your pom.xml (similar in function to the build.xml from Ant) all the librairies your project depends on (example : apache commons) along with their version and Maven get them directly from a repository (by default, the central maven repository)
Then you do not have to manually install any jar to make your project work. All is downloaded and cached on your local machine. You can even create an enterprise repository where you put all the jars needed by your company
Maven uses the concept of artifacts which are pre-built library projects with their own dependencies
To mavenize a project, you'll have to write a pom.xml describing your project (examples are numerous), get rid of your libs directory (or whatever classpath you described under Eclipse) and add all your dependencies to your pom.xml
You could also check Mavenizer for a first-start
But Maven is a lot more what i've just said. Read the docs, read poms from librairies and you'll get used to it quickly ;-)
If you use the M2Eclipse plugin from Sonatype, it's just a matter of right clicking the project in the package explorer and choosing Enable Dependency Management in the Maven menu. You are also advised to adjust the directories that contain the sources to the Maven standard directory layout but if you absolutely can't, you can configure that later.
Apart from that: Well, look for tutorials and documentation (for example there is the free book Better builds with Maven. Maven is very complex (yes, I don't think it is simple) and very powerful.

Migrating from ant to maven in Netbeans

Our software is written in Java and comprise many (7) projects.
These projects are Netbeans ant projects.
I'm considering to converting them to maven2.
Where can I find some hints for doing such thing?
Don't read that book. It will only make you confused. Read this book instead: "Maven - The definitive guide" http://www.sonatype.com/books/maven-book/reference/ .
Also, the maven site has a lot of information, but the structure is terrible so you'll need to use google to navigate in it.
Here is my suggestion:
Do this by hand, not with "automagic" "help" from the IDE. Maven integration doesn't work that good yet, not in any IDE.
Make sure you program project is divided into modules under a common umbrella module, so that each module produces a single binary artifact (jar, war,...) possibly accompanied by the javadoc of the source code behind that artifact, a zip with the source code etc. The basic principle is that each module produces a single artifact, containing all the non-test-code under that module. You can do this while the project is still built by ant.
Each module should conform to the standard maven directory layout. The build destination is under [module]/target/[output-type, e.g. "classes"]. The source code is under [module]/src/main/[src-type e.g. "java"] and [module]/test/[src-type]. The artifact consists of all the code under src/main, and none of the code under src/test, as it built to the target directories. You can do this while the is still built by ant.
Start by transforming the sub-module that has no dependencies on other modules in the project.
Now you can create the parent maven module pom.xml with artifact type "pom", consisting of one of the modules below. Make a child module for the first submodule (the one with only external dependencies), using the umbrella module as "parent". Remember that you need to specify version for the parent. Remember to add the child module as a "module" in the parent too. Always use ${project.version} as version in the child modules when you create multi-module projects like this. All modules under a parent must be released simultaneously in a single operation, and if you use this setting maven will make sure the version fields stay the same across all modules and gets updated everywhere during the release. This may make it difficult to re-use the existing numbering scheme, but that doesn't matter. You are never going to run out of version numbers anyway.
Add the necessary dependencies, and make sure you can build the parent and the child module together using the command "mvn clean install" from the parent module.
Proceed with the rest of the modules the same way. Dependencies to other modules under the same parent project should also use ${project.version} as the "version" they are depending on, meaning "the same version as this". NOTE THAT in order to build, the module you are depending on must be built using "mvn install", so that it gets deployed to you local (computer) repository. Otherwise the depending module will not be able to find the classes. There are NO source-code dependencies between modules in maven, only dependencies to built and packed versions installed in local and remote repositories. This can be very confusing if you come from ant-projects. Build from the root module until you get comfortable with this. It takes two days.
Don't use maven integration in IDEs. It is a bad idea. Use "mvn idea:idea" or "mvn eclipse:eclipse" to set up your workspace as a non-maven ordinary IDE project. The inter-module dependencies mechanisms in maven and the IDE aren't identical and will never be. Also, if you have several mavenized projects with dependencies in between, you want to have several of these in your workspace with dependencies set up between. You can do this with mvn idea:idea / eclipse:eclipse if you create a separate maven project file called "workspace.xml" (or whatever) in the same directory as parent module, set up as a multi-module project containing modules "." and "../otherproject" (only one-way reference here, no parent ref back). If you run "mvn idea:idea / eclipse:eclipse -f workspace.xml" you get a workspace with all these modules linked together. No IDE integration lets you do that. This sound like a lot of extra work, but the workspace.xml-file is really small. It doesn't have to contain all that dependency stuff and all that, only the reference to the modules you want to bind together in your IDE.
I did a succeful migration of NetBeans Ant project to Maven project using the instruccions by Joseph Mocker here: http://forums.netbeans.org/ptopic55953.html
I cite the important part:
close the project
rename the build.xml, nbproject files/folders to something so NB won't recognize them.
close and restart NB (so any memory cache knowledge of the project is gone)
copy in an empty pom from some other project.
open the project back up in NB (NB should now identify it as a maven project)
rearrange the files to follow the maven way (™)
This won't be an easy task since Maven2 expects the files to be organized in a specific way. Anyway Better Builds with Maven is a free book that should get you started. It will help you understand Maven and it also has a chapter on migration.
I discovered that the migration is not necessary. The real requirements that I need was automatic download of dependencies (libraries).
This is also achieved by Ivy which nonetheless uses maven repositories.
I solved converting project from ant to ant+ivy with IvyBeans.
I have built a script to migrate Ant builds to Maven. You can find more information here:
https://github.com/ewhauser/ant2maven
It won't help you with fixing your directory structure and or any additional Ant tasks, but it removes a lot of the tedious steps to get started.

Categories

Resources