how to Include source code inside the mvn built jar - maven

We are changing our projects from ant to mvn build.
In the ant build jar - xyz.jar [we used to have the source files inside]
xyz.sources.jar inside xyz.jar
How can I do the same through pom.xml. I tried maven-source-plugin, but this creates the sources jar inside target folder. I want this sources jar inside output jar.
Thanks.

The convention is to ship these artifacts separately. Offering them separately in a Maven repository allows tools like Eclipse and IntelliJ to match the sources to the binaries automatically, and life is good.
To do what you want to do, you could run the Maven Source Plugin before the main JAR file is packaged (e.g. in the prepare-package phase), and have it write the sources JAR to the target/classes/ folder, and not attach. Like so:
<plugin>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>source-jar</id>
<goals>
<goal>jar</goal>
</goals>
<phase>prepare-package</phase>
<configuration>
<outputDirectory>${project.build.outputDirectory}</outputDirectory>
<finalName>filename-of-generated-jar-file</finalName>
<attach>false</attach>
</configuration>
</execution>
</executions>
</plugin>

Related

Maven exclude only certain files when deploying

Is there a way to exclude files only when calling mvn deploy but have the files included when I call mvn install?
EDIT:
When I run the jar locally I want the logback.xml in src/main/resources but when I deploy it so it's a library the logback.xml should not be included.
It is not the "Maven Way" to have an artifact with different content depending on where it's stored. Maven expects artifact-1.0.jar to be exactly the same in the remote repository and any local repositories.
You could have the project create a classified jar alongside the real jar. The classified jar would include the logback.xml.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<!-- default-jar is the ID assigned to the jar:jar execution included automatically by
Maven. -->
<execution>
<id>default-jar</id>
<configuration>
<!-- not exactly sure of the exact syntax for excludes in the jar plugin -->
<excludes>
<exclude>logback.xml</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>jar-with-logging</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>logging</classifier> <!-- or whatever -->
</configuration>
</execution>
</executions>
This will create two artifacts, artifact-1.0.jar and artifact-1.0-logging.jar. Both artifacts will end up in both repositories. If you don't want the logging version to be attached (Maven terminology for published to repos), investigate using the maven-assembly-plugin which can create packages in various formats without attaching them.
You could also move the logback.xml into a separate project, package it separately, and add it to the classpath only when you run the jar from the local script.

Intellij Idea ignores maven assembly plugin

I have a multimodule project and one module is a web app and the second one has a custom assembly.
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>src</descriptorRef>
</descriptorRefs>
<formats><format>jar</format></formats>
</configuration>
<executions>
<execution>
<id>cfg-src</id>
<goals>
<goal>single</goal>
</goals>
<phase>package</phase>
</execution>
</executions>
</plugin>
Basically I need this archive to contain maven project with sources and maven structure, no binaries etc.
When I assemble WAR with maven i.e. mvn package, everything is fine. I use "src" classifier in dependcy. My WAR contains the jar with correct assembly. But when I deploy WAR with Intellij Idea (I created tomcat run config) assembly config of the project is ignored and I an incorrect one. How do I make Idea not to ignore assembly plugin. Please let me know if I'm clear.
IntelliJ does not run mvn package when you deploy your webapp in Tomcat. It runs its custom Make and Build artifact actions.
You can change this behavior by going into Run/Debug configurations dialog and setting Run Maven Goal (package in your case) as a Before launch action instead of Make and Build artifact.
Hope this helps

how to wildcard attach multiple files to an artifact in maven?

In my maven project, the ant plugin generate multiple war files and I want to attach them all in the same artifact. I tried the build-helper-maven-plugin like this
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>${project.build.directory}/*.war</file>
<type>war</type>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
I don't want to specify each war file separately because the ant plugin is dynamic. Is there a way to do that?
Thanks,
Providing wildcards for single artifacts does not seem to be supported by the Build Helper plugin (btw, if it did, it would likely use the includes/excludes configuration used by Resources Plugin).
I've learned that, if you choose to use Maven, it's best to just adjust your build to "the Maven way."
In this case, you should revise your build to not use the ant war plugin, and instead have a multi-module build with a separate module (sub-project) for each war file.
Alternatively, in the past I have accomplished something like you are doing via the Maven Assembly plugin, where the wars are all shipped together in a single tar/gz file. The archive (which contains each of the wars) is then attached to the build.
Note that you should prefer to have your "web apps" module have a artifact type of "pom." The assembly plugin will attach the archives to the final build.
For more information, I've found that Sonatype's online books are a great resource:
http://www.sonatype.com/Support/Books

Maven Multi Module Project + root pom plugin

I have a maven multi module project why is it when I put this configuration:
<plugin>
<groupId>org.zeroturnaround</groupId>
<artifactId>jrebel-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<id>generate-rebel-xml</id>
<phase>process-resources</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
in the root pom and mvn install the project no rebel.xml file is generated.
I can generate it using mvn org.zeroturnaround:jrebel-maven-plugin:1.1.3:generate but that only creates the rebel.xml under target/classes and does not include it in the jar\war package.
But when I put the above configuration in the individual maven module it does generate it during install and includes it in the package as per process-resources
But I don't want to duplicate the plugin in all modules, but only put it in the root pom and during install is should generate the rebel.xml file and include in the package.
Am I missing how maven works?
Turns out it was my bad I had put the plugin by error in the pluginManagement section when I thought I had put it in the build>plugins section where it should be, now it's working fine. Many Thanks

Deploy additional jars to repo using maven-release-plugin

I'm using zi and maven-release-plugin to generate jar files which I'm attempting to submit to the maven central repo. One of the requirements for inclusion in the central repo is that the artifact have a -javadoc.jar file that contains the generated javadocs. If that's not possible they require that you have an empty -javadoc.jar file in order to pass the automated tests.
I'm generating the empty jar file using exec-maven-plugin and I'm placing it in the correct location, but it's being ignored by maven-release-plugin. As a result it's not being signed by my GPG key and it's not being deployed to the repo.
Is it possible to generate an empty javadoc jar file using the javadoc plugin?
If the javadoc plugin won't generate an empty jar file how do I get the maven-release-plugin to recognize, sign and deploy the jar file which is being generated by my shell script?
Is there some other option that I'm overlooking?
You should try to add the jar-source via the build-helper-maven-plugin which can be used attach artifacts to the cycles.
are you using clojure to write a maven plugin or creating a project written in Clojure?
clojure-maven is a tool for writing maven plugins using clojure:
Maven components to allow the use of clojure when writing maven plugins.
If you are creating a project in Clojure, the zi plugin which was designed for creating clojure projects that are compatible with central, may be what you are after. It's written by the same author (Hugo Duncan).
So I figured it out. Instead of creating the empty jar file via the shell script directly you need to create target/apidocs using the exec-maven-plugin plugin as part of the compile phase.
<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<executions>
<execution>
<id>Generate Empty Javadoc</id>
<phase>compile</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${basedir}/scripts/emtpy-apidocs.sh</executable>
<arguments>
<argument>${project.build.directory}/apidocs</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
Then during package phase you use the javadoc plugin to create the jar. The resulting jar will now be picked up by the release plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>

Resources