deploying a maven project - deployment

I have a maven project and I'd like to create a distribution of it with the dependencies. I've tried the maven-assembly-plugin and built the jar with dependencies, but that unpacked all of the jars and repackaged them all into a big, single jar. What I'd like is something like my jar file and a lib folder that has all of the dependencies. Then when I run it, I could run "java -cp lib/* my.package.MainClass".
What's the best way to go about doing this with maven? Or the recommended way to deploy?
thanks,
Jeff

I have used the Maven assembly just for that in my project.
First enable your plugin in your POM and call your assembly config :
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<!--I recommend 2.1 as later versions have a bug that may
Duplicate files in your archive
-->
<version>2.1</version>
<!--Executes the packaging along with the mvn package phase
-->
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptors>
<!--Relative path to your descriptor -->
<descriptor>src/main/assembly/package.xml
</descriptor>
</descriptors>
</configuration>
</plugin>
Then in your descriptor you can decide how you want your layout to be before you package the whole thing
<assembly>
<!-- this will create an extra resource project-1.1.1-package.zip, you can
choose jar as well in the format-->
<id>package</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<!-- Insert here extra files as configs or, batch files, resources, docs etc-->
<fileSets>
<fileSet>
<directory>src/main/assembly/files</directory>
<outputDirectory>/</outputDirectory>
<includes>
<include>**/conf/*.*</include>
<include>**/doc/*.*</include>
</includes>
</fileSet>
<!-- I like to integrate the jre as well... simplifies my deployement -->
<fileSet>
<directory>target/jre</directory>
<outputDirectory>/jre</outputDirectory>
</fileSet>
</fileSets>
<!-- This will scrub your dependencies and add them to your lib folder, I excluded
Test stuff as it is not needed, could have declared the resource as a test
only phase as well would not have had to exclude it here
-->
<dependencySets>
<dependencySet>
<outputDirectory>lib</outputDirectory>
<excludes>
<exclude>junit:junit</exclude>
</excludes>
</dependencySet>
</dependencySets>
</assembly>
This will create a zip file with the layout you have specified in your output directory config, package the whole thing as a zip file (you can choose zip, jar, war ...) and deploy it in my repository with the rest.
I skipped bits and pieces to make it simpler but my package expands to include batch files, dlls, config, doc and the JRE so everything needed is in the same zip... all is needed to run the thing is extract and click start.bat !
I could also probably make it in to a jar properly formatted with METADATA and just double click the jar itself to start it all, I did not need or have time to toy around this option but you may try it as well.
Beware of versions above 2.1 of the assembly plugin, it will create duplicate entries if your directives enable it to find the same file in different locations, this will give you a lib folder with the same jars repeating twice. not very dangerous as unzipping will collapse them but still annoying to have the unzip ask you if you want to overwrite files. Plus the fact that you do not know which won if somehow they turned out to be different in content.
Maven is great but I find that it is sometimes frustrating to get it working, Plus documentation can sometimes be hard to find and use. However, used appropriately it will save you tons of time.
good luck

See:
http://maven.apache.org/shared/maven-archiver/index.html
You should be able to use the maven-jar plugin to package up an archive, specify the main class to execute along with the classpath. It can generate a manifest file for you for your project.
http://maven.apache.org/shared/maven-archiver/examples/classpath.html#Prefix

Related

Eclipse, Maven and incorrect MANIFEST.MF file being used

This time I have a problem with running my Web App project in Eclipse.
Whenever I'll commit my app to SVN, then build it and deploy using Jenkins to Tomcat container - the app runs fine, and the correct MANIFEST.MF is used.
But, when I want to debug it inside Eclipse (with Tomcat 8.5 container) - there is an deafult manifest file used instead of the one containing my custom properties from pom.xml.
I've defined few variables in pom.xml regarding the application name, version and so on. The manifest file is being generated in /target/generated-resources/m2e-wtp/webResources/META-INF/MANIFEST.MF. This file contain all of my custom properties.
But when I call
resources = getClass().getClassLoader()
.getResources("META-INF/MANIFEST.MF");
The path returned is: /META-INF/MANIFEST.MF. The file indeed exists, but is almost empty:
Manifest-Version: 1.0
Class-Path:
Here's my pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<!-- Exclude an unnecessary file generated by the GWT compiler. -->
<!-- <packagingExcludes>WEB-INF/classes/VAADIN/widgetsets/WEB-INF/**</packagingExcludes>-->
<manifest>
<addClasspath>true</addClasspath>
<addDefaultImplementationEntries>false</addDefaultImplementationEntries>
</manifest>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
<manifestEntries>
<Specification-Title>${project.name}</Specification-Title>
<Specification-Version>${project.version}</Specification-Version>
<App-Version>${build.version}</App-Version>
<Build-Number>${build.number}</Build-Number>
<SVN-Revision>${rev.number}</SVN-Revision>
<Sys-Name>${sys.name}</Sys-Name>
<Sys-Full-Name>${sys.full.name}</Sys-Full-Name>
</manifestEntries>
</archive>
<!-- <packagingExcludes>WEB-INF/classes/META-INF/MANIFEST.MF</packagingExcludes> -->
</configuration>
</plugin>
I've tried almost every suggestion I've googled so far and nothing works :(
Please help!
Thank you in advance
Tom
Okay,
Finally I've found a solution. It turned out that I just need to simply copy a generated MANIFEST.MF file from the genrated wtp output folder to my project META-INF.
During building on Jenkins - the file is getting ovewritten by the generated one anyway so it seems that this makes no harm to project.

how to include third parties library in maven-bundle-plugin(v2.3.7)

I am using servicemix(v4.5.3) and want to deploy my application(depends upon hundreds of third party library) as bundle via maven-bundle-plugin.
Below is my pom.xml
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<extensions>true</extensions>
<executions>
<execution>
<id>wrap-my-dependency</id>
<goals>
<goal>wrap</goal>
</goals>
<configuration>
<wrapImportPackage></wrapImportPackage>
<instructions>
<Include-Resource>{maven-resources}</Include-Resource>
<Bundle-ClassPath>.</Bundle-ClassPath>
<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
<Embed-Transitive>true</Embed-Transitive>
<Import-Package>*</Import-Package>
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.artifactId}</Bundle-Name>
<Bundle-Version>1.0.0</Bundle-Version>
<Bundle-Activator>com.bundle.example.Main</Bundle-Activator>
</instructions>
</configuration>
</execution>
</executions>
</plugin>
I've followed this for creating bundle, but when I execute mvn bundle:wrap than it convert the external jars into bundle and placed into target/classes folder of my project.
Now, my query is should I have to copy all bundle and placed into deploy folder of servicemix installation directory to run my application. I've followed this approach, but still I am getting some error in while my application starts.
Manifest file :
Imported Packages
com.dhtmlx.connector from dhtmlxgridConnector (476)
com.google.gson,version=[1.7,2) -- Cannot be resolved
com.googlecode.ehcache.annotations,version=[1.1,2) -- Cannot be resolved
com.hazelcast.core,version=[2.6,3) from com.hazelcast (437)
com.tinkerpop.blueprints -- Cannot be resolved
com.tinkerpop.blueprints.impls.orient -- Cannot be resolved
com.tinkerpop.frames -- Cannot be resolved
This is just a little part of my Manifest file of bundle. Here some bundle are still unresolved that I think the problem for starting my bundle.
And 2nd query: is there any better approach to handle all 3rd parties libraries while using maven-bundle-plugin.
waiting for some valuable suggestion.
When I need to convert a JAR to a bundle in Servicemix and import I use:
./bin/servicemix
osgi:install -s wrap:file:////"jar_location Ex: /lib/ojdbc6-13.jar"
Execute shutdown command, choose yes option.
Now your JAR will be available as a bundle in ServiceMix.

Replace class files before maven assembly

I'm facing a very unique situation and need some advice:
I'm working on a project which depends on an other project from my same company and now I need to apply some modifications to the dependency.
The problem which I have is that the source code from the dependency has been lost, so the only thing I have is a maven dependency in the repository, with the corresponding jar file.
On top of that, some of the classes in the Jar file where created using JiBX parser, mapping some XSD files which I neither have, and the resulting classes are synthetic and I found no decompiler able to handle them properly.
The only good thing of all of that is that the class which I need to change can be properly decompiled, so went for the following:
I decompiled the whole jar file and ended up with some classes (the
JiBx ones) with empty or wrongly implemented methods.
I commented out the body of the wrong methods to have stub objects, applied the required changes to the right classes and recompiled.
I took the old Jar file, opened it and manually replaced the old class with the new one.
And the resulting Jar worked as expected.
Now my question is: Can I do all of that using Maven?
The idea would be to put the JiBX class files as resources, and keep the stub equivalents as source files and then let maven:
Compile everything as usual, putting all the compiled class files
into target folder
Remove stub class files from target folder and replace them with the old precompiled class files
package the jar.
Which approach would you recommend?
UPDATE
I give some more details about the dependency project structure:
All classes are inside the same package:
my.project.domain.JiBX__c_GeneratedObfuscatedClass1.java
my.project.domain.JiBX__c_GeneratedObfuscatedClass2.java
my.project.domain.JiBX__c_GeneratedObfuscatedClass3.java
my.project.domain.CustomizableClass1.java
my.project.domain.CustomizableClass2.java
my.project.domain.CustomizableClass3.java
JiBX classes are not imported properly as dependency and if I try to put any of the CustmizableClasses into the project source and let the JiBX ones be in a dependency, the compiler reports missing methods.
I also tried using the Shade Plugin as suggested, but since I need to include the JiBX classes into my source path, I will end up having to include into package JiBX classes from jar dependency and compiled CustomizableClasses, but skipping CustomizableClasses from jar dep and compiled JiBX classes.
I looks like it can work, but I admit that I still didn't find the way of doing it.
Any clues will be very welcomed.
UPDATE 2 (RESOLUTION)
I explain here how I finally managed this using the shade plugin as suggested, just in case someone else needs to do the same:
I finally created a project with the decompiled classes inside the same package, and left the methods which didn't want to be decompiled commented out.
In the pom.xml I added the following:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<includes>
<include>${project.groupId}:${project.artifactId}</include>
<include>TheDamnedProject:WithoutSources</include>
</includes>
</artifactSet>
<filters>
<filter>
<artifact>TheDamnedProject:WithoutSources</artifact>
<includes>
<!-- These classes will be taken directly from dependency JAR -->
<include>my/package/ClassWhichCouldNotBeDecompiled1.class</include>
<include>my/package/ClassWhichCouldNotBeDecompiled2.class</include>
<include>my/package/ClassWhichCouldNotBeDecompiled3.class</include>
<include>my/package/ClassWhichCouldNotBeDecompiled4.class</include>
</includes>
</filter>
<filter>
<artifact>${project.groupId}:${project.artifactId}</artifact>
<excludes>
<!-- These classes will be overridden by the ones inside the JAR -->
<exclude>my/package/ClassWhichCouldNotBeDecompiled1.class</exclude>
<exclude>my/package/ClassWhichCouldNotBeDecompiled2.class</exclude>
<exclude>my/package/ClassWhichCouldNotBeDecompiled3.class</exclude>
<exclude>my/package/ClassWhichCouldNotBeDecompiled4.class</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
thanks!
Carles
Here's how I would do that:
Create a new Maven project for this Jar file, with packaging type jar
Include the original Jar file as a dependency
Add the one decompiled .java file in the src folder
This should get you to a point where the .java file can be compiled, since the other classes from the jar file are available.
You now have two Jar files: one original, and one that should just contain the single recompiled and changed class.
Adding both to your application class path might work, but will depend on the order on the classpath.
If you want to end up with one jar file, I recommend to take a look at the Maven Shade Plugin (http://maven.apache.org/plugins/maven-shade-plugin/), which will allow you to create a new Jar file with contents from multiple sources. It will allow you to filter what goes into the new Jar file.
The Maven Shade plugin allows you to specify which classes from each artifact are included. It uses wildcards and include/exclude tags for this, as described here: http://maven.apache.org/plugins/maven-shade-plugin/examples/includes-excludes.html
Once that Jar file is created, I would release it using the Maven Release plugin, and then include that artifact downstream. This will allow you to only update the patched Jar when it is really required, it probably doesn't have to be on every build. But that depends on your usage pattern.
For the version number of the new Jar file, I recommend to use a variation of the original one. Use the same groupId and artifactId, then modify the version number. If the original has 1.0.2, your new, patched file should be released as 1.0.2-1, to indicate that it's based on the 1.0.2 sources. If you need to make an additional change, release that as 1.0.2-2 and so on. This will make it easy to understand which version your patches are based on, and the incrementing patch number will give you a means to distinguis the releases.

GWT. Maven. GWT Module <module_name> not found in project sources or resources

I have a maven multi-module project. One of this modules (compiled as .jar) contains only domain objects, which will be used at client and server sides (I add this .jar as dependency to other my modules).
I know that GWT module, where will be used objects from shared .jar, must also have source files for successful compilation. So I tried to add to my pom.xml both:
<resources>
<resource>
<directory>src/main/java/<path></directory>
<includes>
<include>**/*.java</include>
<include>**/*.gwt.xml</include>
</includes>
</resource>
</resources>
and
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<versionRange>[${gwt.version}]</versionRange>
<goals>
<goal>resources</goal>
</goals>
<plugin>
But resulting .jar don't contain GWT module source (i.e gwt.xml). All sources of domain classes are added as well (at root directory of .jar), but ModuleName.gwt.xml not.
Where is problem? Thanks.
If your .gwt.xml file is in src/main/resources/ then it won't get copied if you specify src/main/java/ as the resource path...
You should probably omit the <resource> section and let the GWT plugin include the source in the jar or at least have two sections, one for the .gwt.xml file (src/main/resources or where you put it) and one for the source code (as you have it now).
Cheers,
I was troubleshooting this error today so I'm just posting my fix:
Multi-module gwt project being build with the maven gwt plugin needs an entry in the pom.xml like:
<modules>
<module>../theothermodule</module>
</modules>
In order to compile.
This error have multiple explanations. Check list:
if you are referencing a gwt module you need to point to the *.gwt.xml file in dot notation without the file extension. E.g. com.example.ThirdParty refers to com/example/ThirdParty.gwt.xml module
to import the 3rd party module, add <inherits name="com.example.ThirdParty" /> to your *.gwt.xml file
the ThirdParty.gwt.xml should contain one or more source elements pointing to translatable code. E.g. <source path='shared' />.
all translatable code in ThirdParty.jar needs to include plaintext *.java sources. E.g. com/example/shared/Widget.class and com/example/shared/Widget.java are both present
the ThirdParty.jar is on your classpath
Notes:
if the ThirdParty gwt module does not have entry point it does not need to be compiled with gwt compiler
the gwt compiler does not require extra configuration to include the ThirdParty module as long as its jar is on classpath and your *.gwt.xml inherits ThirdParty.gwt.xml; the same applies to the gwt maven plugin

Including scala-library.jar in Maven generated package

I'd like to use Maven to include all the dependencies needed to run any Scala programs I write. I imagine this would mean at least scala-library.jar as well as any libraries I may use.
I don't mind where these dependencies are stored (inside the generated JAR or outside), I'm just looking for a solution that sets up stuff like the manifest file classpath and generally requires a minimum amount of manual intervention and boilerplate configuration.
Thanks.
You can use the jar-with-dependencies descriptor format that comes with the Assembly plugin:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
When you run mvn assembly:assembly you'll get a jar with all dependencies (including any necessary Scala libraries) in your target directory.
Use scala-archetype-simple archetype. Here are the list of other archetypes.