Maven/Eclipse: The default build path for resources excluded everything - eclipse

I'm using M2E for maven integration with eclipse. My unit tests reference property files in the resources/ directory. Well, everything fine in the command line test (mvn test). However, in Eclipse, the resources couldn't be found.
Check the Java Build Path, there, all resources entries are marked with Excluded: **. (I deem it should only exclude .java/.class files) Then, after removed the exclude pattern, the problem fixed.
I'm not sure whether I should remove all Excluded ** by hand, or maybe I doesn't use M2E correctly.
P.S. The projects are imported by Existing Maven Projects.

It does this on purpose, to allow the maven-resource-plugin to do the resource copying. You might have filters enabled after all. I have pestered the m2e list about this, you are welcome to file a bugzilla and join in the pestering. it is really annoying.

Related

Eclipse doesn't see classes from Maven modules in workspace

We have some Maven modules shared between several teams, with the mandate to share the source code even though our projects use different dependencies and resources. To accomplish this, we have our modules set up as recommended in Using Maven When You Can't Use the Conventions under "Producing Multiple Unique JARs from a Single Source Directory." Specifically, we have a shared parent module containing the src directory but whose pom declares <packaging>pom</packaging> and only builds the two submodules. Each submodule inherits from this parent and refers to the shared src directory using this:
<build>
<sourceDirectory>../src/main/java</sourceDirectory>
</build>
The two submodules have different artifact ids, allowing dependent modules and projects to specify which version and dependency set they need. It also upholds the Maven principle of "one module, one output."
This all works great in Maven-land: compilation, installation, deployments, etc. What doesn't work well is Eclipse integration. Some things work fine: building the modules, deploying to our Maven repo, pulling in dependencies to build our project. But things such as code completion and jumping to class/method definitions do not work at all. It's as though Eclipse doesn't recognize the source at all.
If we just check out a module from SVN, Eclipse doesn't know about the classes but instead uses jars from the repo. If we then import the modules as Maven modules, they show up in package explorer and the project build path. However, all references to those classes and methods are now flagged as errors by Eclipse. And we still do not have code completion or navigation.
So my questions are these: How can we get Eclipse to recognize the code and do its normal code navigation while still satisfying our varying project requirements? Am I missing some simple Eclipse configuration? Do we need to rework our Maven module structure, and if so, how?
Some additional context: The different dependencies for the projects are rather large, including different major versions for things such as Weblogic and Spring. The Weblogic versions will converge some time next year, but the other dependencies will be slower (and some resource files will likely always remain distinct). So for the near- to mid-future, we have to account for different dependencies between the projects.
We are using profiles to allow our Jenkins server to build both submodules while allowing individual developers to build only the submodule their project needs. Using profiles to manage the dependencies is problematic because we lose transitivity of dependencies.
Update (12/8/15)
I was eventually able to make Eclipse recognize the source directory by using "Link Source..." on the "Configure Build Path..." dialog. Adding a source folder would not let me reference the module's parent directory, but Link Source let me assign an arbitrary directory to use. It's not ideal, but it seems to be working.
I was eventually able to make Eclipse recognize the source directory by using "Link Source..." on the "Configure Build Path..." dialog. Adding a source folder would not let me reference the module's parent directory, which derailed me for a while. However,Link Source let me assign an arbitrary directory to use.
It's not ideal, but it seems to be working. We can now jump to definitions with F3, and errors are now highlighted correctly. It's good enough that I don't feel bad recommending it to the other team. I wish Eclipse would automatically allow a parent source directory to be referenced, but at least the manual intervention worked right.

How to mark .jar file as excluded from publish/export structure?

I have a .war project in Eclipse. It is probably not relevant but I should mention that it was converted from a maven project using mvn eclipse:eclipse plugin.
I saw a warning that various .jar files on the Build Path were not being exported. To fix this I went to Project Properties -> Deployment Assembly and added the jars that needed to go into the .war
However, one .jar file does not need to go into the .war because it will be provided by the container. It is on the build path merely to get the project to compile.
For this .jar file, I want to get rid of the warning. One way was to Ctrl+1 to QuickFix which caused the Properties->Java Build Path->Libraries tab->...jar file to have an extra line on expanding it which says: Excluded from publish/export structure: (None)
My question is how could I have done this without Quick Fix? I could not find any way to exclude from publish/export structure without using the Eclipse Ctrl+1 Quick Fix.
There is no other UI beyond the quick fix for setting the ignore flag.
m2e-wtp automatically takes care of this for you - any dependency that has the scope as provided is excluded from the publish\export structure.

Trying to include a maven built library in Eclipse, giving missing artifact error

I've seen that there are quite a few questions regarding this problem, but unfortunately none have solved it for me. Here is a screencap of what's going on:
It is dependent on the android-support-v4 jar file which as you can see is added to my build path. It the exact jar file from the maven library directory's libs folder. I've tried the following:
Restarting Eclipse
Cleaning the project
Right-clicking the project and updating dependencies
Updating project configuration Disabling and re-enabling workspace resolution
Nothing has helped. Is there any other tricks to getting this resolved? All of the answer's I've seen suggest doing these things that I have already tried.
Thanks a lot!
Actual root cause:
The OP JMRboosties reports in this instance having to desactivate Proguard (the tool which shrinks, optimizes, and obfuscates your code by removing unused code and renaming classes, fields, and methods with semantically obscure names)
disabling proguard on the line where the error occurred (the <plugin> item) in pom.xml solved the problem
(See "How to use ProGuard with android-maven-plugin ").
Certain side-effects can result from using Proguard: For example, the Proguard page does mention:
The default proguard.cfg file tries to cover general cases, but you might encounter exceptions such as ClassNotFoundException, which happens when ProGuard strips away an entire class that your application calls.
Original answer:
As mentioned in this GitHub post:
you need to use maven Android SDK Deployer to install it:
I'm going to deploy my own android artifacts to my personal repository to avoid having this problem again.
If you're not explicitly using Maven you can just import it as a regular Android project into eclipse and it'll pick up the .jar from the libs/ directory.
(Note your android-support-v4.jar isn't in libs in your project)
declare it in your pom.xml.
You have to install both Android 1.6 and the compat lib using the maven SDK deployer for now.
cd to platforms/platform-4/ and extras/compatibility-v4/ in the deployer and run mvn install in each.
Hopefully the compat lib makes it into maven central soon so I can avoid this step.
Again, the project is set up to be used as a normal Android project in eclipse too completely separate from maven.
File, New, Project, Android, use existing sources, select library/ folder.
Assuming you're using m2e-android behind the scenes, the reason you're getting compile problems is that the m2e-android plug-in strips out all provided scope dependencies from the Eclipse project classpath.
We do this because, due to changes in ADT 16.0.0, any JAR file in the Eclipse classpath will be packaged into distributable APK file.

Extract eclipse project .classpath dependencies into ant script

I have a list of Eclipse projects that I would like to compile based on the existing project configuration.
As far as I can tell, if an ant script could read the .classpath files, it would pretty much be able to infer the project dependencies and perform a "javac" compilation in the right order. This would save time in describing the same dependencies again in the ant script or a Makefile.
The dependencies I am interested in are JAR Dependencies, JRE dependencies, and inter-project dependencies. These are -- as far as I can tell -- part of the .classpath XML file.
Any ideas on how Eclipse project dependencies could used in an ant script?
Right click on your Project -> Export
"General/Ant Buildfiles".
Choose the projects and there you go.
Otherwise...
I have some experience with ant4eclipse and it is a hassle to get it stable.
Go check Buckminster or Maven Tycho for a good solution.
I'm currently using Ivy along with Ant, Eclipse and Maven.
I just love the way Ivy works.
Currently, we have a workspace with many projects using Liferay (with Tomcat) for the front-end and Glassfish for the back-end.
We were looking for a way to manage our dependencies a lot better than how we were doing it.
So I took Ivy, replaced all of the classpaths and deployment dependencies in eclipse and was able to build my application using 1 ivy file per project using either Eclipse or Ant.
Ivy integrates like a charm in ant and builds are done either from the workspace or by command line.
I strongly suggest you look at this avenue. Additionnaly, by adding Artifactory, we have a local repository in which the ivy files look for dependencies. This helps us maintain and rule which jars are to be used by developpers. Once everything is setup, we will build our application nightly using Jenkins and these builds will be using our Artifactory repository to resolve dependencies since our build servers do not have access to the internet.
Hope this helped
If you are running the Ant script only from eclipse using the "External Tools Configurations", you can add the variable ${project_classpath} to the Classpath.
Depending on if you are in a plugin project and dependencies you might need to add the
${eclipse_home}.
In case you get an error launching Variable references empty selection: ${project_classpath}, make sure the ant xml file or at least the project is selected. This is important.
I believe the ant4eclipse project provides support for executing Ant builds based on Eclipse metadata files.
However, in my opinion that is doing things back to front. You shouldn't have your build (Ant) depending on your IDE (Eclipse) environment. But it is useful if you can derive your Eclipse environment from your Ant build.
This is an approach used successfully in a team I worked in. We had a helper Ant target which applied XLST to project build.xml files to transform these into Eclipse .classpath files. Thus the Ant build.xml files were the single configuration point for our projects.

Eclipse - JUnit / m2eclipse classpath problem

I'm building a GWT application with Maven for dependency resolution and I've got a strange problem with Eclipse when trying to run JUnit tests. I have a standard maven-like structure of sources and tests:
/src/main/java
/src/test/java
If I set the the Java Build Bath > Default output folder of all source folders to:
/MyProject/target/classes
then there are no problems. However, the GWT plugin needs the sources to reside in:
/MyProject/src/main/webapp/WEB-INF/classes
in order to be able to deploy from this directory in development mode, otherwise it complains. However, if I set the output directory of all source folders to this directory, the tests are failing with ClassNotFoundException.
This can be worked-around if I manually add the directory /MyProject/src/main/webapp/WEB-INF/classes to the JUnit launcher's classpath, so, obviously, the compiled .class files are missing and hence the exception I get. But why doesn't eclipse add the directory to the classpath automatically if I define it to be the default output directory?
I suspect the m2eclipse plugin to be overriding the Default output folder setting, can it be? Because if I set the output folder for /src/main/java to /target/classes and src/test/java to /target/test-classes, everything works. But if any output directory is set outside of these two, it doesn't work.
I would appreciate any ideas, thank you!
m2eclipse has a storied history of ignoring the project classpath settings when running unit tests. It used to be (0.12 version of m2eclipse and earlier, I think), that if you just gave up fighting and set your project default classpaths to but target/classes (for source code), and target/test-classes (for unit test code), everything would be happy. With 0.13 m2eclipse, this no longer works, as the maven builder appears to put things in target/your-artificat-version-SNAPSHOT/web-inf/classes. Basically, the folder for the artifact that maven builds. This really sucks, since that path includes a version number.
m2eclipse has about exhausted my patience ;-).
A tip: To find out what is going on with your unit test class paths, you can always DEBUG as unit test, then right click on the thread in the debug perspective and pull up "properties". This will show you the complete classpath used for the unit tests, and you can see what the sneaky annoying plugins are doing to your classpath.