How to explicitly save cobertura-enabled jars in my maven repo? - deployment

So we have continuous integration in Jenkins of our maven builds. We have a number of module builds, which are packaged together in an assembly build. The assembly build is then a dependency of our integration test build, which installs the full package, starts it, runs tests, and shuts it down. We have nightly cobertura reports running on the module unit tests, but now we want nightly cobertura reports from the integration tests. I'm comfortable with our ability to setup customized builds for the cobertura reporting using the cobertura-maven-plugin.
The problem is, I don't want to do this all the time, so how do I differentiate this jar in the maven repository? Is there some way to use a descriptorRef (like jar-with-dependencies) to get it in the same maven repository but with slightly different coordinates? Then the next question of course is how do I get the assembly build to have different coordinates? Of course this is likely the same or similar answer.

I am not really a maven guy, but I think you can deploy this artfifact specially (I use ivy and have a special deploy configuration for the instrumented jars):
Have a look at classifiers:
Deploy an artifact with classifier
Classifiers are the additional text given to describe an artifact.
artifact-name-1.0-bin.jar artifact-name-1.0-dev.jar
artifact-name-1.0-prod.jar
From the above artifact names, classifiers can be located between the
version and extension name of the artifact.
bin is used to describe that the artifact is a binary.
dev is used to describe that the artifact is for development.
prod is used to describe that the artifact is for production.
To add classifier into your artifact for your deployment, set the text
to the classifier parameter.
mvn deploy:deploy-file -Durl=file:///C:/m2-repo \
-DrepositoryId=some.id \
-Dfile=path-to-your-artifact-jar \
-DpomFile=path-to-your-pom.xml \
-Dclassifier=bin
https://maven.apache.org/plugins/maven-deploy-plugin/examples/deploying-with-classifiers.html

I have not understood correctly but If you would like to use over and over again your aggregated jar you can use install plugin?
Let's make clear steps,
Assemble your file
Install it to your local repository
Use this file
In order to make it you should use install plugin with install-file goal after the phase you aggregate your file.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<phase>anyphase</phase>
<goals><goal>install-file</goal></goals>
<configuration>
<groupId>mygroup</groupId>
<artifactId>myartifact</artifactId>
<version>${project.version}</version>
<packaging>pom</packaging>
</configuration>
</execution>
</executions>
</plugin>
Then simply reference your jar wherever you want with the necessary coordinates.

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>

Force unpacking with maven-dependency-plugin

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

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.

Problems by import of a multiple modules maven 2 project into eclipse workspace

I was wondering if someone has experienced the same problem as me and can help me.
I have a maven project which contains 6 modules. Some of modules are depending on each other. The project is written in Java and builds to jars, wars and aar. I've been trying to import it to Eclipse with the m2eclipse plug-in. It seems to work fine until the project builds. During the build process I get hundreds of errors complaining about missing Java files which are generated. As I found out eclipse can't recognize that some of generated packages should be interpreted as source code. I don't realy know what to do with it as I spent a lot of time already trying to solve this issue. The project is building fine with command line. My target is to debug the whole project on Tomcat server that's why I want to use eclipse as it has a pretty good integration with Tomcat.
Every help would be greatly appreciated.
Thank you!
As documented in the Why generated source folders are not added to classpath entry of the FAQ:
Maven plugins used to generate source
code from resources or other sources
can register additional source folders
to Maven project during the build.
Usually such plugins are bound to
process-resources (or
process-test-resources) build phase
(for example jaxb, modello or xdoclet
plugins). This means that to get those
source folders for generated sources,
we have to run corresponding Maven
build phase.
Not all projects using generated
sources, so for performance reasons,
m2eclipse does not run any Maven goals
by default on project import. This can
be changed in the Maven settings in
"Window > Preferences... > Maven >
Goals to run on project import" (e.g.
you can specify "process-resources"
build phase or specific plugins in
that field).
Alternatively you can run "Maven >
Update project configuration" action
from the project popup menu, which is
configured to run "process-resources"
by default and it can be also changed
on the same preference page.
So either add the goal to which the source generation process is bound to the list of goals to run on import or generate sources by running maven and update the project configuration.
Try using mvn eclipse:eclipse
Under the project where you have additionally generated source. When this is generated by maven it is normally under target folder.
Therefor eclipse:eclipse will recognize this and add as a source folder.
Rembember to refresh the project after this.
Use build-helper-maven-plugin (sample bellow) to tell Eclipse to add a generated folder to the build path :
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources/cxf</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
Add this in the pom of each project that generates sources...