There are solutions available to "disable" the default jar that is generated by the maven-jar-plugin. However, since my final artifacts probably depend on this jar (I am relatively new to maven, so not entirely sure if it is actually the case, but it seems so) when I disable it, my build fails with the following error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-shade-plugin:2.3:shade (build-cli) on project putwb: Failed to create shaded artifact, project main artifact does not exist. -> [Help 1]
Otherwise, it produces an empty jar along with the other jars I am building through my scripts. Can somebody suggest a way to "delete" the default jar, after the build is complete? Alternatively, can someone point out how I can disable that yet my build succeeds?
I am posting the relevant part of my pom.xml below:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>readme-md</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>README.md</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<!-- Build the CLI version -->
<phase>package</phase>
<id>build-cli</id>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<!-- Don't include the UI code -->
<filter>
<artifact>*:*</artifact>
<excludes>in/ac/iitk/cse/putwb/ui/**</excludes>
</filter>
</filters>
<finalName>putwb-cli-${project.version}</finalName>
<transformers>
<!-- Don't include images -->
<transformer implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
<resource>.png</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>in.ac.iitk.cse.putwb.experiment.PUTExperiment</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
<execution>
<!-- Build the UI version -->
<phase>package</phase>
<id>build-ui</id>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>putwb-ui-${project.version}</finalName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>in.ac.iitk.cse.putwb.ui.PUTWb</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
<!-- If I add this to the script, the build fails -->
<!--
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>default-jar</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
-->
</plugins>
</build>
You can use ant-run plugin's delete task. You can execute this plugin in any phase that is later than package phase in the life cycle.(because it will be created again in package phase. Check which one suites you better.) Execute the maven command you choose as this plugins phase (like mvn verify if you choose verify phase)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<delete file="${project.build.directory}/YourJarName.jar"/>
</target>
</configuration>
</execution>
</executions>
</plugin>
Check Build life cycle https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
thanks to #markthegrea for pointing the correct folder.
Related
I created a general shade plugin instance below
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<filters>
<filter>
<artifact>*:*<artifact/>
<excludes>
<exclude>META-INF/*.gif</exclude>
<excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
However when executing mvn clean package I found the original- jar is the same size of the shade jar and contains all the dependency library, what could be the cause of such incident
original-myapp-0.0.1-SNAPSHOT.jar 168Mb
myapp-0.0.1-SNAPSHOT.jar 168Mb
I have a project with maven dependencies and I want to instruct maven to copy some dependencies into a libraries directory and make it merge others directly into the output JAR.
So far I used the maven-dependency-plugin to copy all dependencies into a libraries directory:
<properties>
<libraries.directory>libraries</libraries.directory>
</properties>
<!-- ... -->
<!-- Copy JAR libraries to target folder -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${libraries.directory}</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
This works. However, now I want to exclude a specific groupdId and instead merge that dependency into the output JAR like described in this answer.
I can successfully exclude the groupId with excludeGroupIds but adding a second execution block with an includeGroupIds tag causes the dependency to be copied into a target/dependency subdirectory which is not what I want.
I furthermore use the maven-jar-plugin to build my JAR and reference the libraries directory on the classpath:
<!-- Generate Target JAR -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
!-- Drop the META-INF/maven folder -->
<addMavenDescriptor>false</addMavenDescriptor>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>${libraries.directory}</classpathPrefix>
<mainClass>ApplicationClient</mainClass>
</manifest>
<manifestEntries>
<Built-By>Me</Built-By>
</manifestEntries>
</archive>
</configuration>
</plugin>
Maybe the dependency inclusion in the JAR has to be configured for the maven-jar-plugin instead, how can that be done?
I solved this problem by using the maven-dependency-plugin...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${libraries.directory}</outputDirectory>
<!-- Excluded in the libraries directory -->
<excludeGroupIds>com.mycompany, com.mysecondcompany</excludeGroupIds>
</configuration>
</execution>
</executions>
</plugin>
and maven-shade-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<!-- Included in the fat JAR -->
<includes>
<include>com.mycompany:*:jar:</include>
<include>com.mysecondcompany:*:jar:</include>
</includes>
</artifactSet>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
Irrelevant tags have been removed for clarity.
I want to get some resources from dependency that are not in resources directory, but in src/main/dir.
I tried to use maven-remote-resources-plugin, but I don't know how to change resource directory or even if it's possible.
<plugin>
<artifactId>maven-remote-resources-plugin</artifactId>
<version>1.7.0</version>
<executions>
<execution>
<id>process-remote-resources</id>
<goals>
<goal>process</goal>
</goals>
<configuration>
<resourceBundles>
<resourceBundle>some:dependency:1.0</resourceBundle>
</resourceBundles>
<outputDirectory>${project.basedir}/src/main/dir</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/dir</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
I need this resources before generate-sources phase.
I have an Apache Maven Java project that imports the class:
com.sforce.soap.enterprise.EnterpriseConnection
When running the project from IntelliJ, I have no problems executing the code. However, I want to be able to run the project from the command line as a shaded jar. I have been shading the jar using 'mvn package'. When I run the jar from the command line I am getting the error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sforce/soap/enterprise/EnterpriseConnection
My pom includes dependencies:
<dependency>
<groupId>qa-integration.sfdc</groupId>
<artifactId>enterprise</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/enterprise-1.0.0.jar</systemPath>
</dependency>
<dependency>
<groupId>qa-integration.sfdc</groupId>
<artifactId>metadata</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/metadata-1.0.0.jar</systemPath>
</dependency>
My shade execution looks like the following:
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.sunrun.integration.sfdc.metadata.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
When I list the contents of the jar, I see no salesforce files at all.
I had this problem and my solution was to add the following snippet to my pom.xml:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>corrigo.Main</mainClass>
</manifest>
</archive>
<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>
<!--<plugin>-->
<!--<groupId>org.apache.maven.plugins</groupId>-->
<!--<artifactId>maven-jar-plugin</artifactId>-->
<!--<version>2.4</version>-->
<!--<configuration>-->
<!--<archive>-->
<!--<manifest>-->
<!--<mainClass>corrigo.Main</mainClass>-->
<!--</manifest>-->
<!--</archive>-->
<!--</configuration>-->
<!--</plugin>-->
</plugins>
</build>
Then you just run mvn package.
Here is a reliable resource:
https://www.mkyong.com/maven/how-to-create-a-jar-file-with-maven/
I want to create an executable jar (with all the *.class of my code in it).
However, I don't want the Jar to include the resources that during compilation are in my src/main/resources path.
my project hierarchy is:
project
-src
-main
-resources
-resources_setting.xml
-target
-classes
-resources_setting.xml
I want my jar to include only the classes of main and the dependencies, not the resources inside target\classes or inside resources.
How do I do that?
I am using maven-assembly-plugin, like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>cqm.qa.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
For bundling purpose, I usually used the maven-shade-plugin and the settings are described below. It will work same as assembly plugin.
<profile>
<id>generate-shaded-jar</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>**</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>cqm.qa.Main</Main-Class>
<Class-Path>.</Class-Path>
</manifestEntries>
</transformer>
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>log4j.properties</exclude>
<exclude>details.properties</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
<configuration>
<finalName>cqm-full</finalName>
</configuration>
</plugin>
</plugins>
</build>
</profile>
In the above configuration, I've excluded log4j.properties and details.properties from the final jar with dependencies with name cqm-full.jar
Update
Invoke the profile using mvn install -Pgenerate-shaded-jar
Now resource files from src/main/resources won't get added in cqm-full.jar. If invoked without profile mvn clean install, you can still view the resources in the jar