Unable to disable generation of empty JAR (maven-jar-plugin) - maven

Sometimes, my Talend Open Studio components have resources but not Java sources (they are purely metadata components). I need to disable the generation of JAR files in such a case.
I configured the maven-jar-plugin this way:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<forceCreation>false</forceCreation>
<skipIfEmpty>true</skipIfEmpty>
<useDefaultManifestFile>false</useDefaultManifestFile>
</configuration>
</plugin>
but I still get the ${project.name}.jar file with pom.properties, pom.cml, the manifest and an empty file App.class containing only "class {}"
While I can disable the includes of all maven stuff using this:
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
I still get a JAR with the manifest file inside it
Are there some configuration parameters I misconfigured?

Most efficient way to disable the creation of jars is to configure the maven-jar-plugin like this:
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>default-jar</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
It will place the default jar creation in the none phase, it will never be run.

I found the solution by myself, even if it's only a workaround. I delete the JAR using a delete antrun task if /src/main/java directory doesn't exist:
<!-- remove the empty JAR if not needed -->
<if>
<not><available file="${basedir}/src/main/java" type="dir" /></not>
<then>
<delete file="${project.build.directory}/${project.name}-${project.version}.jar"/>
</then>
</if>
this task requires antcontrib to work properly and, ofc, it doesn't work if you plan to do releases with maven (but it's ok for metadata-only components, like Talend Open Studio plugins)

You can instruct maven-jar-plugin to not generate META-INF/maven/*/pom. files, as explained in Maven Archiver Reference.
Also, you can use its skipIfEmpty option.
Following code combines both these (just to have them copy-paste ready):
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<skipIfEmpty>true</skipIfEmpty>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
</archive>
...
This works fine, but when you do mvn install, it fails due to missing project artifact.
Similar problem will probably be with mvn deploy and with release, but I didn't check these.
However, if you can live with antrun's delete, the property skipIfEmpty will probably work well for you, and is a bit more elegant. At least it does not introduce a new execution and its dependencies etc.

Related

What's the proper way to generate a manifest with DS and maven-bundle-plugin?

I am using today maven-bundle-plugin to generate the manifest of my projects. Due to others constraints, my modules use the "jar" packaging (i can't use the "bundle" packaging), and currently, my pom look like this :
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<executions>
<execution>
<id>bundle-manifest</id>
<phase>process-classes</phase>
<goals>
<goal>manifest</goal>
</goals>
<configuration>
<instructions>
...
</instructions>
</configuration>
</execution>
</executions>
</plugin>
I'd like now to generate a 'Service-Component' header and the DS xml descriptor from my annotated components, but adding "<_dsannotations>*</_dsannotations>" is not working :
Service-Component header is correctly generated, but the xml are not present in the jar
If i rebuild my maven project without a clean goal, then the 'Service-Component' header have duplicates references : After digging in the code, the plugin use the old generated manifest from target/classes/META-INF/MANIFEST.MF and merge it with the new generated one. The 'Service-Component' is then concatened
So, how should i configure my pom for this to work ? For now, i use the 'unpackBundle' option (in order to have the xml in my bundle) and an empty src/main/resource/MANIFEST.MF (in order to bypass the merge of the old manifest) : it looks ugly :-)
Moreover, the 'bnd-maven-plugin' work as intended, but the integration with maven are maybe too light (or not documented?), as 'global configuration' in a parent pom, generation of the Bundle-SymbolicName or Bundle-Name, etc.
Thanks!
There is a newer plugin for maven that is closer to both bnd and maven. This plugin does not take over the jar target and properly follows the maven phases.
Take a look at http://njbartlett.name/2015/03/27/announcing-bnd-maven-plugin.html

Creating stub files for ejb in maven build tool

Team,
Our project makes use of EJB, was 6.1 is our server, While deploying the code , am in need of stub files to be present in EAR (Maven Generated). Inorder to achieve this, am using was6 plug in our pom.xml
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>was6-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>ejbdeploy</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-ejb-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
For this we need to set the environment variables "WAS_HOME".
But we cannot set the wasHome in continuum build server and hence we are getting BUILD ERROR - "wasHome not defined"
Is that any other way to achieve this?
- Creating the stub files for all ejb class without using WAS 6 maven plugin in Maven.
WAS6_HOME environment variable is the default value used by was6-maven-plugin to know where Websphere is located,
but people often use the wasHome attribute to indicate this.
From here it's possible to use Maven properties in the pom or in the settings file and Maven profiles.
On the other hand, you can generate the EJB Stubs without the maven plugin, but you will always need to know where websphere is installed.
Regards
Inorder to achieve this we can set the parameter(ejbdeploy = true) in was6 server install configuration xml
and its worked.
Thanks for all the responses.

How add local dependecy in assembly JAR

I have a multimodule Maven project with several dependencies. I want to build a fat executable JAR containing them as well as my own compiled classes. I found maven-assembly-plugin to be just what I needed except one nasty problem.
Some of my dependencies are local and distributed with project sources. I use system scope for them. It looks something like this:
<dependency>
<groupId>com.intellij</groupId>
<artifactId>forms_rt</artifactId>
<version>1</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/forms_rt.jar</systemPath>
</dependency>
The problem is that for some reason these libraries aren't unpacked and bundled with the rest of dependencies in result JAR.
I know that usage of system scope is considered bad practice, and in fact I even can find some of them (though quite outdated) in Maven repositories, but anyway it puzzles me how it can be solved with maven-assembly-plugin.
Just in case my plugin configuration looks like this:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>repoll.server.Repoll</mainClass>
</manifest>
</archive>
<finalName>${project.build.finalName}-full</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
As I understood I have to write custom descriptor, which e.g. includes and unpacks all JARs from ${project.basedir}/lib directory, but after several unsuccessful attempts I still don't know how to do so.
I've managed to find solution here. It's described in this question.
In short, the whole problem was in usage of system scope. It turned out, that such dependencies are filtered out by default, which I found out by running mvn package with debug output enabled (-X/--debug).
When local repository is defined for these JARs, distributed with project, they are unpacked by maven-assembly-plugin as exepected.

jaxb2-maven plugin

I wanna generated java classes from xsd files bt soome how whenever i run the code it shows the error
No Schema has been found... here is the code... Kindly help...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>src/main/resources/xsd</schemaDirectory>
<includeSchema>**/*.xsd</includeSchema>
<!-- <generatepackage>org.onesync.esb.datasync.model</generatepackage> -->
<!-- The package in which the source files will be generated. -->
<packageName>org.onesync.esb.datasync.model</packageName>
<!-- The working directory to create the generated java source files. -->
<outputDirectory>src/main/java/org/onesync/esb/datasync/model</outputDirectory>
</configuration>
</plugin>
</plugins>
I don't think <includeSchema>**/*.xsd</includeSchema> is valid syntax for jaxb2-maven-plugin:xjc Try omitting that parameter.
If you don't specify schemaFiles it should use all XSD files in the schemaDirectory.
"schemaFiles -- List of files to use for schemas, comma delimited. If none, then all xsd files are used in the schemaDirectory. This parameter also accepts Ant-style file patterns." (see jaxb2-maven-plugin documentation)
BTW, it is usually a good idea to use maven's configuration parameters to refer to a directory. For example, change <schemaDirectory>src/main/resources/xsd</schemaDirectory> to <schemaDirectory>${project.basedir}/src/main/resources/xsd</schemaDirectory>.
Finally, you might also want to refer to this similar SO post.
I am also using maven configuration and spent almost to compile the project stuff. Later on i came to know that it looking for the schema file i.e. schema.xsd.
If you are using the MAVEN configuration then by default you can put the schema file under the resources directory.
But if you want to specify your path for finding the schema file then you can use the includeSchema tag of schemaDescription in plugin configuration.
OR
You can use the effictive pom to search for specific tag also.
Command for effective pom in maven: mvn help:effective-pom
Thanks

Client JAR containing maven dependencies

I'm building a service which contains a client module which is using Spring. The service which will be implementing the client does not contain spring but it has a dependency on the client which has dependencies on Spring. Ideally I would like the client to include the needed Spring dependencies in the JAR but I can't seem to figure out how to accomplish this. I've seen a few different examples of using maven-assembly-plugin but I would prefer to not have to use something other than "mvn clean package" to accomplish this.
Any help is appreciated.
The maven-shade-plugin allows you to build an uber-jar containing some (or all) of your dependencies. It should allow you to do what you need.
By binding the assembly plugin's single goal to the project's build lifecycle, you can accomplish what you want by running mvn clean package.
Cut/pasting the pom configuration to do this from the usage page of the plugin,
<project>
[...]
<build>
[...]
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.2</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
[...]
</project>
Of course, you would tweak either either to use a different predefined descriptor or even use a separate descriptor file.

Resources