Add one additional File as Project Artifact - java

Im trying to make a maven project which has only one xml file as artifact.
Currently i zip the file with this Configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>create-distribution</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>assembly/master.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
But i would like to have the file itself be the artifact.
Is it possible to tell the assemply plugin to just use a file as artifact?

You can use the deploy:deploy-file goal instead. It allows you to upload arbitrary files.

You can use the build-helper-maven-plugin and the goal attach-artifact which looks like this.
<project>
...
<build>
<plugins>
<plugin>
<!-- add configuration for antrun or another plugin here -->
</plugin>
...
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>attach-artifacts</id>
<phase>package</phase>
<goals>
<goal>attach-artifact</goal>
</goals>
<configuration>
<artifacts>
<artifact>
<file>some file</file>
<type>extension of your file </type>
<classifier>optional</classifier>
</artifact>
...
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
But usually I can't recommend that.

Related

How can I get class files from Maven dependency into the target\classes folder

Is there any sort of Maven plugin that allows me to copy the class files for a dependency into the target\classes folder for my project? Currently I have to manually open the jar file that has the dependencies extract the package that I need to copy over and then copy it into the target\class folder for my project.
I need a way to do those ideally within the pom.xml file but I haven't been able to find a solution that works. Any help would be appreciated, Thanks.
This sounds like a terrible idea. If you want to include unpacked dependencies into your project, use the maven assembly plugin or the maven shade plugin.
Use the maven-dependency-plugin. Here is an example of how you can extract the content of a dependency JAR:
<project>
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
<destFileName>optional-new-name.jar</destFileName>
<includes>**/*.class,**/*.xml</includes>
<excludes>**/*test.class</excludes>
</artifactItem>
</artifactItems>
<includes>**/*.java</includes>
<excludes>**/*.properties</excludes>
<outputDirectory>${project.build.directory}/wars</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>true</overWriteSnapshots>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
[...]
</project>
below is the example to use of assembly pulgin and it works for me
below is the child pom plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.4.2</version>
<configuration>
<archive>
<manifest>
<mainClass>my parent package statup class</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Parent pom plugin
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
</build>

How to read java files from target folder in spring boot

I've been trying to read java files that are stored inside target/generated-sources folder. To store these files I have used below plugin in pom.xml file
<!-- For Code Generation -->
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.12.3</version>
<executions>
<execution>
<id>add-source-for-demoapp</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<schemaDirectory>src/main/resources/xsd</schemaDirectory>
<schemaIncludes>
<include>myschema.xsd</include>
</schemaIncludes>
<generateDirectory>target/generated-sources/xjc/workflow</generateDirectory>
<generatePackage>com.websystique.xml.workflow</generatePackage>
<!-- For including equals,hashcode and toString methods in generated code -->
<plugins>
<plugin>
<groupId>org.jvnet.jaxb2_commons</groupId>
<artifactId>jaxb2-basics</artifactId>
<version>0.9.4</version>
</plugin>
</plugins>
<args>
<arg>-Xequals</arg>
<arg>-XhashCode</arg>
<arg>-XtoString</arg>
</args>
</configuration>
</execution>
</executions>
</plugin>
Now this plugin generated few java files and to access those file I have used below plguin inside pom.xml
<!-- For Adding Generated code directory as source folder -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${basedir}/target/generated-sources/xjc/workflow/com.websystique.xml.workflow</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
I have tried several ways to read these files from target folder as shown below. But nothing is working.
Apart from above 2 plugins I'm also using spring-boot-maven-plugin sonar-maven-plugin maven-surefire-plugin
https://www.benchresources.net/jaxb-a-xml-parsing-technique/
Once files are generated then add generated/java/source folder under classpath in eclipse.

How can I build two repackaged jars with Spring Boot

I'm trying to get spring-boot-maven-plugin to build two repackaged jars for two different main classes. I'added two execution blocks with their specific mainClass parameter in the configuration block but it seems that the plugin does not respect it because the configuration block is inside the execution block and not on plugin level and I always get
Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.3.0.RELEASE:repackage failed: Unable to find a single main class from the following candidates [com.mystuff.tools.b4commandline.Application, com.mystuff.tools.loadtester.Application]
here's the plugin section of maven-spring-boot-maven plugin of the pom.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>${artifactId}</id>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>com.mystuff.tools.loadtester.Application</mainClass>
<finalName>${artifactId}</finalName>
</configuration>
</execution>
<execution>
<id>b4-commandline</id>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>com.mystuff.tools.b4commandline.Application</mainClass>
<finalName>b4-commandline</finalName>
</configuration>
</execution>
</executions>
</plugin>
Any help on this would be great. I saw recipes on stackoverflow for the maven-plugin but those do not apply to spring-boot.
You can do this with <classifier>. Example:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.3.RELEASE</version>
<executions>
<execution>
<id>Pack application 1</id>
<phase>package</phase>
<configuration>
<finalName>application</finalName>
<mainClass>com.test.Application1</mainClass>
<outputDirectory>target/application1</outputDirectory>
<classifier>1</classifier>
</configuration>
<goals>
<goal>repackage</goal>
</goals>
</execution>
<execution>
<id>Pack application 2</id>
<phase>package</phase>
<configuration>
<finalName>application</finalName>
<mainClass>com.test.Application2</mainClass>
<outputDirectory>target/application2</outputDirectory>
<classifier>2</classifier>
</configuration>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
I could not make it work both within a single build (I'm not saying it is not possible though), however one option is to define 2 maven profiles:
<profiles>
<profile>
<id>one</id>
<activation>
<property>
<name>one</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>default</id>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>com.xyz.LauncherOne</mainClass>
<finalName>one</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>two</id>
<activation>
<property>
<name>two</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>default</id>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>com.xyz.LauncherTwo</mainClass>
<finalName>two</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Then, build it twice using both profiles (activated with a property in my code sample):
$ mvn -Done clean install
$ mvn -Dtwo clean install
It creates: one.jar and two.jar
Assuming its doable, doing what you are asking would mean that the 2 jars produced would be identical except their manifest's main-class attribute. I would rather suggest you to package a single jar and use spring profiles to launch it:
$ java -jar -Dspring.profiles.active=profile1 YourApp.jar
$ java -jar -Dspring.profiles.active=profile2 YourApp.jar
This allows you to define 2 application-${profile}.properties, and #Conditional configuration classes, all based on the profile name.
It seems that you must consider first execution without id and another executions with id; Then it is working.
for me below working fine:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<!--<id>Batch 1(Don't put this one)</id>-->
<phase>package</phase>
<configuration>
<classifier>1</classifier>
<finalName>Core</finalName>
<mainClass>com.mainclass1</mainClass>
</configuration>
<goals>
<goal>repackage</goal>
</goals>
</execution>
<execution>
<id>Batch 2</id>
<phase>package</phase>
<configuration>
<classifier>2</classifier>
<finalName>BatchA</finalName>
<mainClass>com.mainclass2</mainClass>
</configuration>
<goals>
<goal>repackage</goal>
</goals>
</execution>
<execution>
<id>Batch 3</id>
<phase>package</phase>
<configuration>
<classifier>3</classifier>
<finalName>BatchB</finalName>
<mainClass>com.mainclass3</mainClass>
</configuration>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<testFailureIgnore>true</testFailureIgnore>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>

Exclude packages while generating sources jar in Maven

I would like to generate a sources jar but without some packages in my project.
Now I have this in my pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>sources</id>
<goals>
<goal>jar</goal>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
How to exclude specific packages?
similar but with netbeans environment
You can configure the maven-source-plugin with the excludes attribute:
List of files to exclude. Specified as fileset patterns which are relative to the input directory whose contents is being packaged into the JAR.
A sample configuration where you would exclude every class under the package com.my.package would be the following:
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>sources</id>
<goals>
<goal>jar</goal>
<goal>test-jar</goal>
</goals>
<phase>package</phase>
<configuration>
<excludes>
<exclude>com/my/package/**</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>

How to make single zip archive with maven assembly plugin with predefined "src" descriptor?

I was explicitly asked to make a package with the predefined "src" descriptor, but I can't get it to work.
This is my maven-assembly-plugin config in my POM:
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<configuration>
<descriptorRefs>
<descriptorRef>src</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- append to the packaging phase. -->
<goals>
<goal>single</goal> <!-- goals == mojos -->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
I can make jar files with no problem, but I don't know how to produce the zip file. I am using eclipse, what option should I choose?
I also have a jar plugin which seems to make the jar all the time:
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
You have to really execute the plugin, cause in pluginManagement the plugin is not really executed. So you have to change the following:
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<descriptorRefs>
<descriptorRef>src</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- append to the packaging phase. -->
<goals>
<goal>single</goal> <!-- goals == mojos -->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
into this:
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>src</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- append to the packaging phase. -->
<goals>
<goal>single</goal> <!-- goals == mojos -->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Afterwards just call
mvn clean package

Categories