Maven shade plugin isn't placing dependency class files into jar - java

My Maven project uses an external library as a dependency, com.sk89q.intake:intake, which I'm trying to package into my jar via the maven-shade-plugin. When building the project, the resulting jar does not contain any of the class files of com.sk89q.intake:intake. During the build process, I get this message, but the build continues on and succeeds:
[INFO] --- maven-shade-plugin:2.4.2:shade (default) # EventManagerPlugin
[INFO] No artifact matching filter com.sk89q.intake:intake
Why is this happening? I'm able to download, access, and use the dependency in my project, so there shouldn't be anything wrong with naming of the artifact.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>deletethis.eventmanager</groupId>
<artifactId>EventManagerPlugin</artifactId>
<version>1.0.0-beta1</version>
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>maven.sk89q.com</id>
<url>http://maven.sk89q.com/repo/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.8.8-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.8.8-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sk89q.intake</groupId>
<artifactId>intake</artifactId>
<version>4.2-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.5</version>
<configuration>
<archive>
<manifestEntries>
<Built-By>deletethis</Built-By>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>com.sk89q.intake:intake</artifact>
<includes>
<include>com/sk89q/intake/**</include>
</includes>
</filter>
</filters>
<relocations>
<relocation>
<pattern>com.sk89q.intake</pattern>
<shadedPattern>deletethis.eventmanager.lib.com.sk89q.intake</shadedPattern>
</relocation>
</relocations>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
As you can see, I am including the com.sk89q.intake:intake artifact. I have looked through the maven-shade-plugin documentation and don't see what I'm doing wrong. The naming is consistent with everything I have found online; that is, groupId:artifactId.
I have also tried building without the <relocation> class relocation tags to see if they were interfering.
It may be useful to know that I'm using M2Eclipse and building with the clean install goals.

The problem is that your are declaring the com.sk89q.intake:intake dependency with the provided scope.
Provided dependency are expected to be provided by the container at runtime so the maven-shade-plugin will not add it to your shaded jar. As such, you need to remove the provided scope from the dependency declaration:
<dependency>
<groupId>com.sk89q.intake</groupId>
<artifactId>intake</artifactId>
<version>4.2-SNAPSHOT</version>
</dependency>
Relevant build log after this change:
[INFO] --- maven-shade-plugin:2.4.2:shade (default) # test ---
[INFO] Including com.sk89q.intake:intake:jar:4.2-SNAPSHOT in the shaded jar.
[INFO] Including com.google.guava:guava:jar:18.0 in the shaded jar.
[INFO] Including com.google.code.findbugs:jsr305:jar:3.0.0 in the shaded jar.

Related

How do I get JaCoCo to work with Maven and JUnit 4.11?

I'm having trouble getting JaCoCo to work with Maven. I keep running into either
Skipping JaCoCo execution due to missing execution data file.
Or
The parameters 'rules' for goal org.jacoco:jacoco-maven-plugin:0.8.2:check are missing or invalid
I also can't seem to get JaCoCo to run with just mvn clean test instead I have to run mvn clean test jacoco:report or mvn clean test jacoco:check
I've tried a variety of methods of editing my POM file, such as adding configuration for destFile and dataFile, as well as the POM settings here: https://howtodoinjava.com/junit5/jacoco-test-coverage/ and here https://www.lambdatest.com/blog/reporting-code-coverage-using-maven-and-jacoco-plugin/ . Any help would be greatly appreciated. Below is my POM file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.NAME.PROJECTNAME</groupId>
<artifactId>PROJECTNAME</artifactId>
<version>1.0-SNAPSHOT</version>
<name>PROJECTNAME</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>17.0.1</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.2</version>
<configuration>
<destFile>${basedir}/target/coverage-reports/jacoco-unit.exec</destFile>
<dataFile>${basedir}/target/coverage-reports/jacoco-unit.exec</dataFile>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- attached to Maven test phase -->
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<configuration>
<mainClass>HelloFX</mainClass>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
This is because you are using <pluginManagement> tag, and <plugins> are put inside it.
As per the Maven documentation on <pluginManagement>,
pluginManagement: is an element that is seen along side plugins. Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one. However, this only configures plugins that are actually referenced within the plugins element in the children or in the current POM. The children have every right to override pluginManagement definitions.
In short, you will use <pluginManagement> in case of multi-module Maven project. Here is a bit more explanation in this answer.
I removed the <pluginManagement> tag from pom.xml and now the build is working and Jacoco report is getting generated.
-> mvn clean verify
...
[INFO] --- jacoco-maven-plugin:0.8.2:prepare-agent (default) # PROJECTNAME ---
[WARNING] The artifact xml-apis:xml-apis:jar:2.0.2 has been relocated to xml-apis:xml-apis:jar:1.0.b2
[INFO] argLine set to -javaagent:/home/codejournal/.m2/repository/org/jacoco/org.jacoco.agent/0.8.2/org.jacoco.agent-0.8.2-runtime.jar=destfile=/tmp/maven-jacoco/target/coverage-reports/jacoco-unit.exec
...
...
[INFO] --- jacoco-maven-plugin:0.8.2:report (report) # PROJECTNAME ---
[INFO] Loading execution data file /tmp/maven-jacoco/target/coverage-reports/jacoco-unit.exec
[INFO] Analyzed bundle 'PROJECTNAME' with 1 classes
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) # PROJECTNAME ---
[INFO] Building jar: /tmp/maven-jacoco/target/PROJECTNAME-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.141 s
[INFO] Finished at: 2022-04-24T02:18:30+05:30
[INFO] ------------------------------------------------------------------------
-> cat target/site/jacoco/jacoco.csv
GROUP,PACKAGE,CLASS,INSTRUCTION_MISSED,INSTRUCTION_COVERED,BRANCH_MISSED,BRANCH_COVERED,LINE_MISSED,LINE_COVERED,COMPLEXITY_MISSED,COMPLEXITY_COVERED,METHOD_MISSED,METHOD_COVERED
PROJECTNAME,io.codejournal.maven.jacoco,Hello,7,0,0,0,3,0,2,0,2,0

class files not created under maven target directory in IntelliJ

I have created a simple maven project to create shaded jar. I built it using mvn clean compile .It creates jar file but after extracting it i don't see .class files corresponding to my project java source files. I see clearly below message when i build but don't see any .class files under target dir.
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) # PersonalizationFeeder ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 10 source files to Documents/PersonalizationFeeder/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
What is the problem here? My pom.xml looks like this.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>data-analytics</artifactId>
<groupId>com.wooplr</groupId>
<version>0.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>PersonalizationFeeder</artifactId>
<packaging>jar</packaging>
<properties>
<userlib.dir>Documents/userlibs</userlib.dir>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.10</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-streaming-kafka_2.10</artifactId>
<version>${spark.version}</version>
</dependency>
<dependency>
<groupId>com.wooplr</groupId>
<artifactId>jedis</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${userlib.dir}/jedis-2.7.3.jar</systemPath>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>junit:junit</exclude>
<exclude>org.apache.maven:lib:tests</exclude>
</excludes>
</artifactSet>
<filters>
<filter>
<!--<artifact>*:*</artifact>-->
<excludes>
<excludeScope>system</excludeScope>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Problem solved...!! In one of the dependency jars, there is licenced jar and because of which it couldn't build properly. To solve the problem,I had to extract that particular dependency jar and remove .RSA,.DSA and *.SF files from META-INF/ dir and then make it jar using jar -cf command and then use this new jar as dependency. It worked fine for me. Thanks all.

Java project compiles with Maven but Eclipse still shows errors

I am new to Maven and an using it to build my Java project. I have two questions:
I was able to successfully compile my project with Maven but Eclipse still reports compile time errors. I know these errors are because I have not added external jars to Eclipse's build path, but is there any other way I can resolve these errors?
How do I run my Java project with Maven?
Here's my pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springhibernate</groupId>
<artifactId>SpringHibernateAssignment</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringHibernateAssignment</name>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>com.springhibernate</groupId>
<artifactId>SpringHibernateAssignment</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<file>${basedir}/lib/ojdbc14.jar</file>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings
only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-install-plugin
</artifactId>
<versionRange>
[2.4,)
</versionRange>
<goals>
<goal>install-file</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
The m2e plug in for Eclipse will integrate Maven so that Eclipse will automatically include JAR's from the local Maven cache based on the contents of the POM.xml.
And excluding all Java source files explains why Maven's build succeeds without any dependencies.
Put the third party libraries as dependencies in your pom.xml.
<dependencies>
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxr-api</artifactId>
<version>1.0</version>
</dependency>
<dependencies>
You can find them in here.
If those jars you mention are public frameworks, you should be able to find them in the maven central repository and add them to your project just like Eranda explained. If you have jars that cannot be found publicly, you have to add them first to your local maven repo using maven install-file plugin: https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
May be your M2_Home is not set, can you confirm. You may be building the project from the command line not from the eclipse ( Guess).
Suggestions:
1. Set M2_HOME.
2. Clean or Build the Project from eclipse, it will add all your dependent jars to references

Maven 3: Overlay is not a dependency of the project

I'm trying to test the overlay functionality of the maven-war-plugin. Basically I need to merge two war projects.
So I defined a war as dependency:
<dependency>
<groupId>my.group.id</groupId>
<artifactId>my-legacy-war-project</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
And then configured the overlay:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<overlays>
<overlay>
<groupId>my.group.id</groupId>
<artifactId>my-legacy-war-project</artifactId>
<targetPath>legacy</targetPath>
</overlay>
</overlays>
</configuration>
</plugin>
But Maven fails to build this project, complaining about this dependency:
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-war-plugin:2.3:exploded (default) on
project my-project: overlay [ id my.group.id:my-legacy-war-project] is
not a dependency of the project. -> [Help 1]
The overlay is supposed to work with Maven 3.0.5? Why the build is complaining about a dependency that's declared?
Not sure why, but using id instead of groupId and artifactId in the overlay worked:
<configuration>
<overlays>
<overlay>
<id>my-legacy-war-project</id>
<targetPath>legacy</targetPath>
</overlay>
</overlays>
</configuration>
I had the same error, but possibly for a different reason, since you are bringing in a war dependency. In my case, I had a war dependency as one overlay, and a jar dependency as another. The build complained about the jar dependency:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.4:war (default-war) on project overlay: overlay [ id com.mycompany:launcher] is not a dependency of the project.
I fixed the error by adding a <type>jar</type> element to my jar overlay. According to the overlay documentation, the default value for type is war, and so the build correctly complained that I did not have a war artifact named launcher.
Here's the working pom for my overlay project:
<project>
<artifactId>overlay</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>app</artifactId>
<type>war</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>launcher</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<overlays>
<overlay>
<groupId>com.mycompany</groupId>
<artifactId>app</artifactId>
</overlay>
<overlay>
<groupId>com.mycompany</groupId>
<artifactId>launcher</artifactId>
<type>jar</type> <!-- THIS IS THE FIX -->
</overlay>
</overlays>
</configuration>
</plugin>
</plugins>
</build>
I had the same problem with maven-war-plugin version 2.2 and abuse of duplicate plugin declaration. After unifying them and using Sergio Michels suggestion, now it works fine using version 2.3 of maven-war-plugin.
Before changing:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<dependentWarExcludes>'**/jdbc.properties,**/hibernate.cfg.xml,**/sql-map-config.xml,**/web.xml,WEB-INF/classes/META-INF/**'</dependentWarExcludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>my-snapshot</warName>
<overlay>
<overlay>
<id>my-webapp-common</id>
<groupId>xyz.mycompany</groupId>
<artifactId>my-webapp-common</artifactId>
</overlay>
</overlays>
</configuration>
</plugin>
After applying changes:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warName>my-snapshot</warName>
<overlays>
<overlay>
<overlay>
<id>my-webapp-common</id>
<targetPath>legacy</targetPath>
</overlay>
</overlays>
<dependentWarExcludes>'**/jdbc.properties,**/hibernate.cfg.xml,**/sql-map-config.xml,**/web.xml,WEB-INF/classes/META-INF/**'</dependentWarExcludes>
</configuration>
</plugin>
Expanding on the other answers. The problem is to get overlay to use the same id as dependency.
Using $ mvn dependency:list can show you the ID you need. For example:
[INFO] +- com.foo.bar.v2:api:jar:1.0:system
[INFO] \- com.foo.bar.v2:main-server:war:1.0:system
[INFO] \- com.foo.bar.v2:second-server:war:classes:1.0:system
Shows one jar and one war. Note: :jar vs :war is entirely controlled by whether you used <type>war</type> in your dependency. Similarly, :classes (or empty) is entirely controlled by whether you used <classifier>classes</classifier> in your dependency.
You need to get this in alignment with <overlay>. For com.foo.bar.v2:main-server:war:1.0:system listed above, this would be the overlay entry:
<overlay>
<id>com.foo.bar.v2:main-server:war:1.0</id>
<groupId>com.foo.bar.v2</groupId>
<artifactId>main-server</artifactId>
</overlay>
For com.foo.bar.v2:second-server:war:classes:1.0:system, this would be the correct entry:
<overlay>
<id>com.foo.bar.v2:main-server:war:1.0</id>
<groupId>com.foo.bar.v2</groupId>
<artifactId>main-server</artifactId>
<classifier>classes</classifier>
</overlay>

Install Jar Dependency to Local Repository as Part of Maven Build Process

I have a third-party jar that is a dependency of my project. Because of business constraints, I do not have access to an enterprise or company repository, which would definitely be my preference for this issue. But regardless, this third-party jar is not available publicly, and so it is included in the web project under src\main\resources.
This is a Maven project, and so I list this third-party jar as a compile time dependency, and include in my pom a build plugin that will install the third-party jar to my local repository as part of the build process; I tell Maven to perform this goal during the validate phase of the build lifecycle, which to my knowledge would be before any other Maven phase.
However, when I run a clean install and have my local repository cleared out, the build fails due to the third-party jar not being resolvable locally or in the Maven central repository. As far as I know, I have set up the pom correctly and I should be seeing Maven attempt to install the third-party jar locally before it begins dependency resolution.
The issue is that if the dependency is listed before the jar has ever been installed locally, the build will fail due to being unable to resolve that dependency. If I remove the third-party jar declaration and run the build, after dependency resolution occurs (which is the very first thing it does after the clean), but before any other phase, it will locally install the jar and things are fine. But to my knowledge, it should run the validate phase before it collects and resolves dependencies, and so the jar should be locally installed before it's resolved by Maven. Any ideas or thoughts?
My pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Web Services</name>
<description>This project will handle communication.</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<!-- This plugin installs the Evip jar from the project's resource folder to the local
repository for normal Maven consumption -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<inherited>false</inherited>
<executions>
<execution>
<id>install-evip-jar</id>
<phase>validate</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>mvn</executable>
<arguments>
<argument>install:install-file</argument>
<argument>-Dfile=${basedir}\src\main\resources\EVIPSoapServer.jar</argument>
<argument>-DgroupId=com.company</argument>
<argument>-DartifactId=EVIPSoapServer</argument>
<argument>-Dversion=1.0.0</argument>
<argument>-Dpackaging=jar</argument>
</arguments>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<!-- EXCLUDE EVIPSOAPSERVER JAR FROM CLASSES DIRECTORY -->
<packagingExcludes>
${basedir}\src\main\resources\EVIPSoapServer.jar
</packagingExcludes>
<webResources>
<!-- INCLUDE SOURCE FILES WITH WAR -->
<resource>
<directory>${project.build.sourceDirectory}</directory>
<targetPath>WEB-INF/classes</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<!-- mvn clean install tomcat:run-war to deploy Look for "Running war
on http://xxx" and "Setting the server's publish address to be /yyy" in console
output; WSDL browser address will be concatenation of the two: http://xxx/yyy?wsdl -->
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<id>start-tomcat</id>
<goals>
<goal>run-war</goal>
</goals>
<phase>pre-integration-test</phase>
<configuration>
<port>${test.server.port}</port>
<path>/webservice</path>
<fork>true</fork>
<useSeparateTomcatClassLoader>true</useSeparateTomcatClassLoader>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<projectNameTemplate>[artifactId]-[version]</projectNameTemplate>
<wtpmanifest>true</wtpmanifest>
<wtpapplicationxml>true</wtpapplicationxml>
<wtpversion>2.0</wtpversion>
</configuration>
</plugin>
<!--This plugin's configuration is used to store Eclipse m2e settings
only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.codehaus.mojo
</groupId>
<artifactId>
exec-maven-plugin
</artifactId>
<versionRange>
[1.2.1,)
</versionRange>
<goals>
<goal>exec</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<!-- COMPILE DEPENDENCIES -->
<dependency>
<groupId>com.company</groupId>
<artifactId>EVIPSoapServer</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.7.7</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.7.7</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.7.RELEASE</version>
<scope>compile</scope>
</dependency>
<!-- PROVIDED/TEST DEPENDENCIES -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
The solution from http://randomizedsort.blogspot.com.es/2011/10/configuring-maven-to-use-local-library.html is based on having a file-based, project-scoped maven repository. Thus, the library is under your source code versioning. Install is one-time manual process, rather than a something specified in the pom.xml file
Three steps:
Create a folder in your project where you will keep the repo. Say lib.
Use Maven to install your jar to the lib directory.
mvn install:install-file -Dfile=path_to_mylib.jar ^
-DgroupId=com.mylib ^
-DartifactId=mylib ^
-Dversion=1.0 ^
-Dpackaging=jar ^
-DlocalRepositoryPath=path_to_my_project/lib
3.Update your pom.xml
<repositories>
<repository>
<!-- DO NOT set id to "local" because it is reserved by Maven -->
<id>lib</id>
<url>file://${project.basedir}/lib</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.mylib</groupId>
<artifactId>mylib</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
Saw this question while looking for a solution to a similar problem. I know it's old, but wanted to share my solution. I ended up solving my problem by using the maven-install-plugin directly:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>whatevs</id>
<phase>validate</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>${oracle.version}</version>
<packaging>jar</packaging>
<file>${basedir}/dev-setup/lib/ojdbc6.jar</file>
</configuration>
</execution>
</executions>
</plugin>
Works perfectly.
I suggest you modeling the external dependency as a separate project (think of it as a wrapper). Your current project may then be dependent on your own project that as part of its build can download the external JAR and package it into its own distributable.
Alrighty, arguing and preference aside, I did go with Sander's recommendation; it was the only one that really worked without custom Mojos, etc. I have a parent Maven project with a packaging of type pom and all it does is install the third-party jar (which could be any number of jars or dependencies) to my local repository. The parent pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>project-evip</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>EvipSoapServerJar</name>
<packaging>pom</packaging>
<modules>
<module>webservices</module>
</modules>
<build>
<plugins>
<!-- This plugin installs the Evip jar from the project's lib to the local
repository for normal Maven consumption -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<inherited>false</inherited>
<executions>
<execution>
<id>install-evip-jar</id>
<phase>validate</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>mvn</executable>
<arguments>
<argument>install:install-file</argument>
<argument>-Dfile=${basedir}\src\main\resources\EVIPSoapServer.jar</argument>
<argument>-DgroupId=com.company</argument>
<argument>-DartifactId=EVIPSoapServer</argument>
<argument>-Dversion=1.0.0</argument>
<argument>-Dpackaging=jar</argument>
</arguments>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings
only. It has no influence on the Maven build itself. -->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.codehaus.mojo
</groupId>
<artifactId>
exec-maven-plugin
</artifactId>
<versionRange>
[1.2.1,)
</versionRange>
<goals>
<goal>exec</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Then, I created a Maven module under the parent Maven project and used the org.apache.cxf.archetype:cxf-jaxws-javafirst:2.7.7 archetype. I simply list the third-party jar as a compile-time dependency, and it's resolved and added to the resulting war. The child module's pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>project-evip</artifactId>
<groupId>com.company</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.company</groupId>
<artifactId>webservices</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Web Services</name>
<description>This project will handle communication.</description>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webResources>
<!-- INCLUDE SOURCE FILES WITH WAR -->
<resource>
<directory>${project.build.sourceDirectory}</directory>
<targetPath>WEB-INF/classes</targetPath>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<!-- mvn clean install tomcat:run-war to deploy Look for "Running war
on http://xxx" and "Setting the server's publish address to be /yyy" in console
output; WSDL browser address will be concatenation of the two: http://xxx/yyy?wsdl -->
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<id>start-tomcat</id>
<goals>
<goal>run-war</goal>
</goals>
<phase>pre-integration-test</phase>
<configuration>
<port>${test.server.port}</port>
<path>/webservice</path>
<fork>true</fork>
<useSeparateTomcatClassLoader>true</useSeparateTomcatClassLoader>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<configuration>
<projectNameTemplate>[artifactId]-[version]</projectNameTemplate>
<wtpmanifest>true</wtpmanifest>
<wtpapplicationxml>true</wtpapplicationxml>
<wtpversion>2.0</wtpversion>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<!-- COMPILE DEPENDENCIES -->
<dependency>
<groupId>com.company</groupId>
<artifactId>EVIPSoapServer</artifactId>
<version>1.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.7.7</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.7.7</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.7.RELEASE</version>
<scope>compile</scope>
</dependency>
<!-- PROVIDED/TEST DEPENDENCIES -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
I appreciate all the help, thank you.
Corresponding to gregm's answer I'm using now:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<packaging>jar</packaging>
<file>${project.build.directory}/${project.build.finalName}.jar</file>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
This installs the JAR of the project itself to local repository.
Result:
--- maven-jar-plugin:2.3.2:jar (default-jar) # SportyLib ---
Building jar: ...-1.0-SNAPSHOT.jar
--- maven-install-plugin:2.5.2:install (default-install) # SportyLib ---
Installing ...-1.0-SNAPSHOT.jar to .../.m2/repository/.../1.0-SNAPSHOT/...-1.0-SNAPSHOT.jar
Installing .../pom.xml to .../.m2/repository/.../1.0-SNAPSHOT/...-1.0-SNAPSHOT.pom

Categories