can maven assembly plugin picking the specify java package? - maven

Suppose we have following packages in our project
org.xxx.dao, org.xxx.service, org.xxx.service.impl
all the package are in ther source folder src/main/java
can i assembly packages into seprated jars,(xxx-dao.jar,xxx-service.jar,xxx-service-impl.jar)?

You can use the standard maven-jar-plugin for this:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>dao</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<archive>
<index>true</index>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
<classifier>dao</classifier>
<includes>
<include>org/xxx/dao/**/*</include>
</includes>
</configuration>
</execution>
<execution>
<id>service</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<archive>
<index>true</index>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
<classifier>service</classifier>
<includes>
<include>org/xxx/service/**/*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
This gives you one JAR with everything and then several JARs with only the selected content which you can select with a classifier in your Maven POMs.
If you want to omit the default JAR (which contains everything), then you replace one <id> with <id>default-jar</id> and add an <excludes> element because the default JAR includes everything, so you have to get rid of anything that you don't want.

maven-assembly plugin is not intended for this kind of tasks, see its features. If you have the source codes, then I would suggest copying them into a module of your project.
But if you want to do it with maven-assembly plugin, then I think you can do it by creating separate descriptor components and defining which files to include in which assembly. You can define source files to be included in includes tags under sources definition.

Related

Not able to add resources using Maven jar plugin

I have a src folder out of which i need to create multiple jars. Like each package as a separate jar. Also i need to put specific xml files from src folder into specific jars. I created a pom which creates multiple jars successfully using multiple executions in plugin. But the problem is the movement of xml files to jars is not happening. Have provided the pom file below.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>tra_common_id</id>
<goals><goal>jar</goal></goals>
<phase>package</phase>
<configuration>
<finalName>tra_common</finalName>
<encoding>ISO-8859-1</encoding>
<classifier>tra_common</classifier>
<includes>
<include>com/**/common/**</include>
<include>com/**/apm/*Access*</include>
</includes>
<excludes>
<exclude>com/**/management/**</exclude>
</excludes>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
</execution>
<execution>
<id>tra_client_id</id>
<goals><goal>jar</goal></goals>
<phase>package</phase>
<configuration>
<finalName>tra_client</finalName>
<encoding>ISO-8859-1</encoding>
<classifier>tra_client</classifier>
<includes>
<include>com/**/client/**</include>
</includes>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
</configuration>
</execution>
</executions>
</plugin>

Include only certain packages in a Maven generated non-runnable jar

I'm trying to build a non-executable jar file that only includes certain packages from my project to use as a library for common code. I'm planning on deploying that to Nexus and including it in other projects.
I can make it manually in eclipse by right clicking on the project, selecting Export, selecting JAR file (NOT Runnable Jar File) then checking the packages that I want to include. I'm trying to mimic this behavior in Maven. I have been able to replicate the non-runnable part in maven by not including a main in the manifest, but still have to find out how to include only certain packages.
This project actually builds 2 jar files already (from 2 different main's in the program - a test GUI and the regular application). I'm having trouble finding information on how to do the non-runnable jar and selecting certain packages only.
Here is the existing build portion of my pom file is below. Build-third is the non-runnable jar:
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>build-first</id>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>com.joy.fb20.da.exec.jDaExecutive</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>DaExecutive</finalName>
</configuration>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
<execution>
<id>build-second</id>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>com.joy.fb20.gui.jExampleAppGui</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>ExampleAppGui</finalName>
</configuration>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<execution>
<id>build-third</id>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>ddslib</finalName>
</configuration>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</plugin>
</plugins>
</build>
Tried this myself and it is possible.
Create a new maven profile
Add pluginManagement section to that new profile for maven-jar-plugin and if needed for maven-resources-plugin also
Use this pluginManagement to configure inclusions/exclusions of sources/resources and override artifacts final name here to have different name for that separate jar
Example
<profiles>
<profile>
<id>subset</id>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<finalName>subset</finalName>
<includes>
<include>org/example/stuff/**</include>
</includes>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>
Using this profile maven creates subset.jar that has everything under org/example/stuff/**
See more about excluding: How to exclude a set of packages from maven build jar?
Of course it might be possible to do all this also without pluginManagement but in my opinion pluginManagement provides clean & flexible way to configure this kind of stuff so i recommend that.
You should be able to do that by using a custom assembly descriptor and specifying your fileset and / or file includes / excludes. See https://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_fileSet

Minify maven plugin,does not add minified file to maven local repository

I'm using minify maven plugin with following configuration in POM -
<plugin>
<groupId>com.samaxes.maven</groupId>
<artifactId>minify-maven-plugin</artifactId>
<version>1.7.4</version>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<executable>${JAVA_HOME_1_7}/bin/javac</executable>
<compilerVersion>1.7</compilerVersion>
</configuration>
<executions>
<execution>
<id>crf</id>
<phase>package</phase>
<configuration>
<jsSourceDir>crf/js</jsSourceDir>
<jsSourceIncludes>
<jsSourceInclude>*.js</jsSourceInclude>
</jsSourceIncludes>
<jsTargetDir>crf/js</jsTargetDir>
<skipMerge>true</skipMerge>
<jsEngine>CLOSURE</jsEngine>
<nosuffix>true</nosuffix>
<closureCreateSourceMap>false</closureCreateSourceMap>
</configuration>
<goals>
<goal>minify</goal>
</goals>
</execution>
<execution>
<id>bundle-configuration-minify</id>
<configuration>
<jsEngine>CLOSURE</jsEngine>
<closureCreateSourceMap>false</closureCreateSourceMap>
</configuration>
<goals>
<goal>minify</goal>
</goals>
</execution>
</executions>
</plugin>
When i build the component(Say 'component-war' using mvn clean install), i see the target(component-war) has a minified JavaScript.But Maven local repo does not contain the minified file.
Due to which when i package the project(Say 'project-ear') target does not contain the minified file.
I Also have the following configuration in my POM -
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<packagingIncludes>WEB-INF/lib/urlrewritefilter-*.jar,WEB-INF/lib/xercesImpl-*.jar,WEB-INF/lib/io-*.jar,WEB-INF/lib/xtags-*.jar,**/*.xml,**/*.xsd,**/*.pdf,**/*.txt,**/*px,**/*dummyfile,**/*.properties,**/*.class,**/*.png,**/*.css,**/*.js,**/*.jsp,**/*.jspf,**/*.xsl,**/*.html,**/*.htm,**/*.vm,**/*.tld,**/*.gif,**/readme,**/*.zip,**/*.jpg,**/*.zul,**/*.zs,**/*.eot,**/*.svg,**/*.ttf,**/*.woff,**/*.swf
</packagingIncludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
First you must delete:
<execution>
<id>crf</id>
<phase>package</phase>
because for WAR project in phase package maven-war-plugin has priority over other plugins (http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Plugins) - WAR is build before minification.
BTW, it explains why Maven has phase prepare-package and do not have post-package; everything defined in package is effectively post-package
That's not enough, because maven-war-plugin will overwrite minified files. You must configure it to ignore original JS files, because their minified versions are already prepared by minify plugin (Exclude source files from WAR package)
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<packagingIncludes>WEB-INF/lib/urlrewritefilter-*.jar,WEB-INF/lib/xercesImpl-*.jar,WEB-INF/lib/io-*.jar,WEB-INF/lib/xtags-*.jar,**/*.xml,**/*.xsd,**/*.pdf,**/*.txt,**/*px,**/*dummyfile,**/*.properties,**/*.class,**/*.png,**/*.css,**/*.js,**/*.jsp,**/*.jspf,**/*.xsl,**/*.html,**/*.htm,**/*.vm,**/*.tld,**/*.gif,**/readme,**/*.zip,**/*.jpg,**/*.zul,**/*.zs,**/*.eot,**/*.svg,**/*.ttf,**/*.woff,**/*.swf
</packagingIncludes>
<!--HERE **************** -->
<warSourceExcludes>crf/js/*.js</warSourceExcludes>
<!--HERE **************** -->
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
BTW, your maven war plugin configuration (packagingIncludes) suggests you have a mess in project. You should consider clean it up, because my proposal extends configuration and any further unnecessary extension cause whole configuration hard to understood

Maven: properties-file alongside jar

I am packaging my application as jar.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>...</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
And also I have my .properties-file placed inside of src/main/resources. I can freely move this file to any other place. I don't want properties-file to be included into jar, but rather placed into the same output directory (where we get jar) side-by-side. What is the best practice for this in Maven?
Okay, one can use goal resources:copy-resources of maven-resources plugin.
I am just leaving standard resources folder for embedded (into jar) resources. And for external resource I create another folder: src/main/external-resources. Here I put my .properties-file.
Following piece will copy external resources to output dir upon validate phase. You can alter the phase per your own needs.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
<resources>
<resource>
<directory>src/main/external-resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

How to define the order of executions using maven-assembly-plugin

I am working on the migration of a project from Ant to Maven. The final distribution I need to deliver is a zip containing an executable jar with all its depencencies. Here is part of my pom:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-4</version>
<configuration>
<finalName>ProjectDistribution</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>fullQualifiedNameToMainClass</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
<descriptors>
<descriptor>${project.basedir}/src/main/assembly/dep.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
<execution>
<id>dist</id>
<phase>assembly</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
And here is the assembly file:
<assembly>
<id>dist</id>
<formats>
<format>zip</format>
</formats>
<!-- 1st approach-->
<!--files>
<file>
<source>
/target/ProjectDistribution.jar
</source>
<outputDirectory>/</outputDirectory>
</file>
</files-->
<fileSets>
<!-- 2nd approach-->
<!--fileSet>
<directory>/target</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.jar</include>
</includes>
</fileSet-->
<fileSet>
<directory>/HelpFiles</directory>
<outputDirectory></outputDirectory>
<includes>
<include>*.*</include>
</includes>
</fileSet>
</fileSets>
I run 1.- mvn compile, 2.- mvn package, and 3.- mvn assembly:single
The problem I am dealing with is that
It does generate the jar with all the dependencies and it does generate the zip but it does not includ the jar in the zip. I pretty much need to figure out a way of making the assembly first generate the jar and wait until it is created (because its size is 5 MB) and then create the zip. Right now the 1st and 2nd approaches -from the assembly file- are commented out, however, I have used both and none of them seem to work.
Any help will be greatly appreciated!
Eric
To get this working you need to split the <configuration> and put it into the two plugin executions:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-4</version>
<executions>
<execution>
<id>jar-with-dependencies</id>
<phase>verify</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>ProjectDistribution</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>fullQualifiedNameToMainClass</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</execution>
<execution>
<id>dist</id>
<phase>verify</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>${project.basedir}/src/main/assembly/dep.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
The first execution will create the jar file. The second execution will take that JAR file and
put it into the ZIP file together with the other files. With this configuration, you can just execute mvn verify or mvn install to create the assembly.
There are two other things to consider:
You should use the verify phase to build your assembly because the jar-with-dependencies descriptor includes the project artifact itself. During the package phase the project artifact will not be ready for packaging
The jar-with-dependencies descriptor has very limited capabilities to create a JAR file with all dependencies. You should use the maven-shade-plugin instead.
You are mixing the pre-defined jar-with-dependencies with a custom zip descriptor. You would normally want one of them - not both.
It looks like you want a zip which contains your project artifact along with its dependencies. For this you would not need to create a jar-with-dependencies. If, however, you do want a single executable jar with all the dependencies in it, then it is not clear why you need to zip it again.

Resources