Force unpacking with maven-dependency-plugin - eclipse

Im am using Maven 3.2.1 with Eclipse Kepler. I have a dynamic web project which has a dependency to another component which includes some JSPs. I now want the JSPs from the dependency to become part of the web root of the dynamic web project. I chose to accomplish this by using unpack goal of the maven-dependency-plugin.
I added a plugin definition to unpack the JARs into /target/m2e-wtp/web-resources. Unfortunatley Eclipse from time to time cleans this folder and teh JSPs are gone. In order to unpack them again I have to delete the target/dependency-maven-plugin-markersfolder. Otherwise the plugin will not unpack the files again.
Is it possible to force the unpacking and ignore the plugin markers?
Is there a better way to get web resources from a dependency into my Dynamic Web Project?

Yes there's a better way, but it's not compatible with Tomcat's "serve module without publishing" feature (or Weblogic's equivalent thing)
Remove your maven-dependency-plugin configuration and add your dependency as a war overlay instead. If your dependency is a war, it'll be automatically recognized as an overlay (http://maven.apache.org/plugins/maven-war-plugin/overlays.html). If it's a zip or a jar, you need to add a specific configuration to your maven-war-plugin definition. Something along :
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<overlays>
<overlay>
<!-- /!\ must also be added as a project dependency-->
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<type>jar</type>
<targetPath>relative/path/to/contextroot</targetPath>
</overlay>
</overlays>
</configuration>
</plugin>
Before deployment, resources will be unzipped under target/m2e-wtp/overlays/bar-version.jar/ and then deployed onto your application server

Related

How do I exclude Maven resources when publishing from Eclipse WTP to a server?

How do I make WTP exclude Maven resources in the src/main/resources folder, when it deploys or publishes to a server? For example, I created a few folders named src/main/resources/qa and src/main/resources/prod, and in there I have properties files. I want to use them for deployment, but I don't want them in the WAR file artifact.
By adding a resources stanza to my pom.xml, I can exclude those folders from Maven builds (e.g. when I run mvn package), and they won't show up in my WAR file artifact.
But, when I use the "Debug As.." approach with WTP to let Eclipse manage and attach to my local Tomcat server, I can see that WTP is publishing all the resources including my excluded folders in the local Tomcat server. I have watched the wtpwebapp folder, that is the deployment target WTP is using, disappear when I have removed my WAR artifact from the Eclipse server definition. Then, the excluded resources find their way back when I add the project back to the server.
I have tried explicitly excluding the folders with "**/qa/" and "**/prod/" entries in the Java Build path for my project on the appropriate source libraries, and in fact, recreating the Eclipse project using mvn eclipse:clean eclipse:eclipse -Dwtpversion=2.0 will add the build path exclusions for me automatically. They just don't seem to be honored when WTP publishes.
#Greg,
One option to consider is having Maven instead of Eclipse WTP push the WAR to the server.
This might entail tradeoffs depending on what Eclipse is doing for you behind the scenes.
Then again, your desktop will more exactly mirror what happens in your CI build.
Anyway, this guide explains in good depth how to configure the tomcat7:deploy goal.
Here is a suggested pom.xml snippet to get you started:
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>deploy</phase>
<goals>
<goal>redeploy</goal>
</goals>
<configuration>
<url>http://localhost:8080/manager/text</url>
<server>TomcatServer</server>
<path>/mkyongWebApp</path>
</configuration>
</execution>
</execution>
</plugin>
</plugins>

Maven/Eclipse - Quick iteration acceptance Testing a project packaged as a WAR file

Eclipse makes working with multi module maven projects easy because you don't have to re-build and re-install a module before dependent modules will see changes to it. So you just change the code and eclipse updates the dependencies magically in the background.
I want to achieve this behaviour for acceptance testing as well.
I have
storage-service
storage-service-war
storage-service-acceptance-tests
If I use embedded jetty or tomcat to test inside the storage-service-war project then obviously code changes are immediately viewable in the tests, but I cannot see any way to achieve the same quick iteration of testing when testing from storage-service-acceptance-tests.
Every way I look at it it seems that I have to build storage-service-war and then use the artefact generated from that, but it seems like overkill when you only want to change one line.
Does anyone have a good method for doing this?
Cheers
Piers
So answering my own question :D The solution I came up with will not work on CI it will likely only work when doing a local build as it makes use of the relative paths of the projects. At the bottom I outline a more robust but more complex approach that should satisfy eclipse and CI.
I was looking at setting attachClasses to true for the war plugin configuration of the war project.
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<attachClasses>true</attachClasses>
</configuration>
</plugin>
</plugins>
You can then reference the jar in the dependent project as follows
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>storage-service-war</artifactId>
<version>${project.version}</version>
<classifier>classes</classifier>
</dependency>
Then I was thinking I could run my tests from within the acceptance test module using embedded jetty or tomcat and pointing them to the web.xml defined in the war project using a relative path.
This works fine with maven via the commandline but fails in eclipse :(
The problem with this is that the jar produced by attach classes is not picked up by the eclipse m2e integration see -https://bugs.eclipse.org/bugs/show_bug.cgi?id=365419 unfortunately it wont be fixed.
So My solution for the moment is to manually add the storage-service-war project to my acceptance test project build path in eclipse. Its not great but it works
The above solution is a bit hacky but the alternative outlined is a bit more involved.
By splitting the project into the following I think it would be possible to have correct eclipse integration and projects that work on CI
storage-service
storage-service-core
storage-service-war
storage-service-acceptance-tests
storage-service-config
The core project contains the logic and source of the webapp and is of type jar, the config contains the web.xml and any other config files and is also of type jar. Then the acceptance-tests and war project are both of type war and serve merely to package the core project into a war and extract the config to the webapp/WEB-INF dir so that they may share a common setup.

Eclipse and m2e - test classpath

I have Spring project backed up by maven and I am using m2e plugin. Now, some of my files are in the /src/main/webapp/WEB-INF/ folder. Now, the problem is that when I am running tests some of the .xml files with configuration don't work because paths to the resources are breaking.
Firstly, question. When I do on project Run As -> JUnit test, dose eclipse backs up by doing mvn test, or is it maven agnostic this way? If so this would explain why after adding
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<additionalClasspathElements>
<additionalClasspathElement>/src/main/webapp/</additionalClasspathElement>
</additionalClasspathElements>
</configuration>
</plugin>
to my pom.xml when I run the test using Run As -> JUnit test and printing classpath I am not seeing my added location, but doing mvn test I can see this location added to classpath.
So, is it possible to make eclipse/maven copy some additional files to target/classes or target/test-classes
or
just add some folders to the runtime classpath when testing the applcation.
Also, is it possible to do such thing in one place so it does not matter if I am running tests using Eclipse gui or just mvn test from console.
Unless this has changed recently, your class path in eclipse is still built from your .classpath file.
m2e injects some elements, notably dependencies, but source/resource paths still need an entry.
Adding what you need to your .classpath should solve the issue.

Eclipse and Maven: Run goal after a file is changed

I have a maven goal configured in the pom that is executed on compile phase.
But also I need that goal to run after a specific file is changed to keep everything always up to date.
i.e I want to save the file "objects.xml" and run the goal "transform" to apply XSL every time a change is done.
Is there a way for doing that from inside Eclipse?
I've been reading about custom builders for the project, but that does not cover my case.
You can add builders and custom ant scripts to your eclipse workspace.
Project->Preferences->Builders
These builders can be automatically be added to you project with
mvn eclipse:eclipse
By adding the folling config to your pom
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<additionalBuildcommands>
<buildcommand>your.custom.Builder</buildcommand>
</additionalBuildcommands>
for more info how to do this see the maven-eclipse-plugin website
http://maven.apache.org/plugins/maven-eclipse-plugin/

Get source jar files attached to Eclipse for Maven-managed dependencies

I am using Maven (and the Maven Eclipse Integration) to manage the dependencies for my Java projects in Eclipse. The automatic download feature for JAR files from the Maven repositories is a real time saver. Unfortunately, it does not include API documentation and source code.
How can I set up Maven to automatically also get the source and javadoc attachments and register them properly with Eclipse?
I am sure m2eclipse Maven plugin for Eclipse - the other way around - can do that. You can configure it to download both the source files and javadoc automatically for you.
This is achieved by going into Window > Preferences > Maven and checking the "Download Artifact Sources" and "Download Artifact JavaDoc" options.
mvn eclipse:eclipse -DdownloadSources=true
or
mvn eclipse:eclipse -DdownloadJavadocs=true
or you can add both flags, as Spencer K points out.
Additionally, the =true portion is not required, so you can use
mvn eclipse:eclipse -DdownloadSources -DdownloadJavadocs
The other answers on this work, but if you want to avoid having to remember command line arguments, you can also just add to the downloadSources and downloadJavadocs config to the maven-eclipse-plugin section of your pom.xml:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
... other stuff ...
</configuration>
</plugin>
</plugins>
</build>
...
</project>
I prefer not to put source/Javadoc download settings into the project pom.xml file as I feel these are user preferences, not project properties. Instead, I place them in a profile in my settings.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<profiles>
<profile>
<id>sources-and-javadocs</id>
<properties>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>sources-and-javadocs</activeProfile>
</activeProfiles>
</settings>
Right click on project -> maven -> download sources
If the source jars are in the local repository and you are using Eclipses maven support the sources are getting automatically attached. You can run mvn dependency:sources to download all source jars for a given project. Not sure how to do the same with the documentation though.
If you are using meclipse do
window --> maven --> Download Artifact Sources (select check)
(If you still get attach source window, then click on attach file button and close the attach source window. The next time you try to see the source it will open the correct source)
There is also a similiar question that answers this and includes example pom settings.
I tried windows->pref..->Maven But it was not working out. Hence I created a new class path with command mvn eclipse:eclipse -DdownloadSources=true and refreshed the workspace once. voila.. Sources were attached.
Source jar's entry is available in class path. Hence new build solved the problem...
in my version of Eclipse helios with m2Eclipse there is no
window --> maven --> Download Artifact Sources (select check)
Under window is only "new window", "new editor" "open perspective" etc.
If you right click on your project, then chose maven--> download sources
Nothing happens. no sources get downloaded, no pom files get updated, no window pops up asking which sources.
Doing mvn xxx outside of eclipse is dangerous - some commands dont work with m2ecilpse - I did that once and lost the entire project, had to reinstall eclipse and start from scratch.
Im still looking for a way to get ecilpse and maven to find and use the source of external jars like servlet-api.
Changing pom for maven-eclipse-plugin to include source/javadoc just apply for new dependencies being added to pom. If we need to apply for existing dependencies, we must run mvn dependency:sources. I checked this.
Checking download source/javadoc in Eclipse-Maven preference, sometimes is not enough. In the event maven failed to download them for some reason (a network blackout?), maven creates some *.lastUpdated files, then will never download again. My empirical solution was to delete the artifact directory from .m2/repository, and restart the eclipse workspace with download source/javadoc checked and update projects at startup checked as well.
After the workspace has been restarted, maybe some projects can be marked in error, while eclipse progress is downloading, then any error will be cleared.
Maybe this procedure is not so "scientific", but for me did succeded.
I've added the pom configuration to the maven-eclipse plugin to download source and javadocs, but I figure/hope that will happen for new dependencies, not existing ones.
For existing dependencies, I browsed in package explorer down to the "Maven Dependencies" and right-clicked on commons-lang-2.5.jar, selected Maven | Download Sources and... nothing appeared to happen (no progress bar or indication that it was doing anything). It did, however, download as I'm able to jump to source in commons-lang now.
overthink suggested using the setup in the pom:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
... other stuff ...
</configuration>
</plugin>
</plgins>
</build>
...
First i thought this still won't attach the javadoc and sources (as i tried unsuccessfully with that -DdownloadSources option before).
But surprise - the .classpath file IS getting its sources and javadoc attached when using the POM variant!
For Indigo (and probably Helios) the checkboxes mentioned above are located here:
Window -> Preferences -> Maven
I had a similar problem, and the solution that worked best for me was to include the source in the same jar as the compiled code (so a given directory in the jar would include both Foo.java and Foo.class). Eclipse automatically associates the source with the compiled code, and automatically provides the JavaDoc from the source. Obviously, that's only helpful if you control the artifact.
After Setting the Properties either at Project Level or User Properties level,
Please do a Maven -> Update Project (Force Update). It downloads the sources
A Small addition to the answer, if your project is not a maven project still you can get the source code of the jars, by using this plugin provided in eclipse
Java Source Attacher