I'm using Maven's exec:java to run jline for one of my projects (current POM attached below). The project used to be a single component, so all dependencies were in the same POM as the exec:java plugin definition. This worked great and all the dependencies were picked up and put on the classpath when I ran 'mvn exec:java'. However, I've now split up the project into a few modules and would like the dependencies from each module to be picked up when exec:java is run, but I can't figure out how to configure it. Advice would be greatly appreciated!
thanks,
Nick
<?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>
<name>Lensfield</name>
<groupId>org.lensfield</groupId>
<artifactId>lensfield-pom</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<includeProjectDependencies>true</includeProjectDependencies>
<includePluginDependencies>true</includePluginDependencies>
<executableDependency>
<groupId>jline</groupId>
<artifactId>jline</artifactId>
</executableDependency>
<mainClass>jline.ConsoleRunner</mainClass>
<arguments>
<argument>clojure.lang.Repl</argument>
</arguments>
</configuration>
<dependencies>
<dependency>
<groupId>jline</groupId>
<artifactId>jline</artifactId>
<version>0.9.94</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<modules>
<module>lensfield-share</module>
<module>lensfield-build</module>
<module>lensfield-webapp</module>
</modules>
</project>
You can specify a parent POM for the project and define the exec-plugin in the pluginManagement section of the parent. This means that the plugin configuration will be available to any child POM that declares a minimal plugin configuration. The following would be sufficient.
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
</plugin>
When the child is processed it will inherit the configuration from the parent, and the exec-plugin will be executed with the current project's dependencies.
Related
I have been trying, for several days now, to create an executable jar file for my muli-module maven project, sadly I have had no luck on doing it.
I know that there are a lot of similar questions already, but, even by following the answers, i cannot manage to run the jar I make.
I am using the maven-assembly plugin, so that my jar contains all the required dependencies, here is the 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>tam-authentication</artifactId>
<name>tam-authentication</name>
<description>authentication tam project</description>
<parent>
<groupId>com.netcomgroup.eu</groupId>
<artifactId>TAM</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<java.version>11</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>"fully qualified class name"</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
...
dependencies from other modules
...
</dependencies>
</project>
futhermore i have another probably related problem regarding jar creation with a common library included:
using Eclipse as IDE, whenever I run as > maven install on the multimodule project, I often get jars failing over correct imports, that I need to delete and import again to complete the java building process correctly. Sometimes i simply must run maven install several times in a row to make the jar building process succeed.
I don't know if the second problem is related but i guess there is some mistake I cannot see in the multi module project jar building.
First of all, as per your pom.xml you did not provide the fully qualified Main class name in your dependency. Second if you provided the Main class name, then after "mvn clean install", it would create 2 jars - one with artifact name and one with "jar-with-dependencies" and you have to user "jar-with-dependencies".
Also i don't think you need to use pluginManagement tag as an extra step. I have just modified a bit your pom below, please try this and check.
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
<artifactId>tam-authentication</artifactId>
<name>tam-authentication</name>
<description>authentication tam project</description>
<parent>
<groupId>com.netcomgroup.eu</groupId>
<artifactId>TAM</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
<java.version>11</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>{Your fully qualified Main class eg - abc.cde.Main}</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
...
dependencies from other modules
...
</dependencies>
I am using maven jacoco plugin for code coverage. I decided to exclude some classes from jacoco coverage report. I found here, how to do it.
So my updated pom file looks like this:
<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>
<artifactId>payment-rest</artifactId>
<packaging>war</packaging>
<name>payment-rest</name>
<parent>
<artifactId>payment-ws</artifactId>
<groupId>com.example.foo</groupId>
<version>1.0.0-INTEGRATION-SNAPSHOT</version>
</parent>
<properties>
<jacoco.line.coverage>0.78</jacoco.line.coverage>
<jacoco.branch.coverage>1.00</jacoco.branch.coverage>
<servlet.version>3.0.1</servlet.version>
</properties>
<dependencies>
<!-- all dependecies here-->
</dependencies>
<build>
<finalName>payment-ws</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
**/com/example/foo/payment/configuration/**.*
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<attachClasses>true</attachClasses>
<classesClassifier>classes</classesClassifier>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
So, when I am running mvn clean install command, I gets this error ():
Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.19:test failed: The forked VM terminated without properly saying goodbye. VM crash or System.exit called?
If I remove exclusion part, project builds successfully.
Can someone advise me how can I solve this issue?
issue is occur because maven-surefire-plugin is missing into your project pom.xml. you need to add this plugin into pom.xml, after adding need to update and rebuild the project and run it.
you can find the maven plugin below:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
</plugin>
</plugins>
you can refer higher version from 2.19.1.
In a Spring Boot based project of mine I want to create two different builds from the same project.
The decision on which build is generated should come from a maven profile.
I want to create one build (full) which includes a certain folder src/main/java/com/example/demo/full and a certain dependency, and a second build (default or light) build which does not include them.
Including the dependencies for build full works, but I don't know how to make sure the folder src/main/java/com/example/demo/full is only compiled for the full build.
Here my pom.xml:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<finalName>demo</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>full</id>
<dependencies>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
How can I manage to have the mentioned source-folder only compiled for profile full?
Add a second src folder like scr\foo and then add a profile in maven configure this src folder.
<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">
<build>
...
</build>
<profiles>
<profile>
<id>extraSource</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>src/foo/</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Here the source folder is added using the Build Helper Plugin plugin for maven. As it is embedded in the build section of the specific profile, it is only active while executing maven with this profile (see the activation section)
there are problem with disabling one of your maven-source-plugin if this dependency is a part of parent which you cant not give ID to, ill recomend to use phase none with this code to one of your pom.xml files that will disable this.
I also recommend to use command: mvn -Prelease-profile help:effective-pom
to print if you have two of dependencies maven-source-plugin in your code, if yes, disable one of them with this code below:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
I'm adding the jspc plugin like this in my 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/maven-v4_0_0.xsd">
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.codehaus.mojo.jspc</groupId>
<artifactId>jspc-maven-plugin</artifactId>
<version>2.0-alpha-3</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<inputwebxml>${basedir}/src/main/webapp/WEB-INF/web.xml</inputwebxml>
<sources>
<directory>${basedir}/src/main/webapp/jsp</directory>
<includes>
<include>**/*.jsp</include>
</includes>
</sources>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.mojo.jspc</groupId>
<artifactId>jspc-compiler-tomcat6</artifactId>
<version>2.0-alpha-3</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
But, when I run mvn clean dependency:copy-dependencies install, I don't see any classes are generated. From here, http://hasini-gunasinghe.blogspot.com/2011/09/how-to-use-pre-compiled-jsps-in-webapp.html, I'm supposed to see a target/jsp-source directory, but I don't have it.
Any problem with my pom.xml?
The problem is that you are declaring the plugin inside <pluginManagement> and it is not declared inside a <plugins>. Quoting Maven documentation (emphasis mine):
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
which is not the case here.
As such, you just need to remove the <pluginManagement> element and let the plugin be declared directly inside <plugins>.
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo.jspc</groupId>
<artifactId>jspc-maven-plugin</artifactId>
<version>2.0-alpha-3</version>
<!-- rest of configuration -->
</plugin>
</plugins>
</buil>
I am using addjars-maven-plugin to add all the jars present in web-inf/lib to my classpath and i am able to build my web-application and package it as war.
Is there any similar way to add all the jars present in some folder to classpath, while building java classes and package it as jar?
I tried the same plugin as below. But, when i try to build my project, the jars are downloaded to my local repository. But still i get class not found exception.
Kindly help. Thanks in advance!
<build>
<plugins>
<plugin>
<groupId>com.googlecode.addjars-maven-plugin</groupId>
<artifactId>addjars-maven-plugin</artifactId>
<version>1.0.2</version>
<executions>
<execution>
<goals>
<goal>add-jars</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${project.basedir}/src/main/extlib</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Full pom.xml content:
<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.xyz</groupId>
<artifactId>testlogger</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<build>
<plugins>
<plugin>
<groupId>com.googlecode.addjars-maven-plugin</groupId>
<artifactId>addjars-maven-plugin</artifactId>
<version>1.0.2</version>
<executions>
<execution>
<goals>
<goal>add-jars</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>${project.basedir}/src/main/extlib</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I could achieve adding the jars present in any folder by using addjars-maven-plugin as above but, the only change i had to make is change the version of the plugin from 1.0.2 to 1.0.5. Thanks all for the support!!
I had the same challenge with addjars-maven-plugin. Calling mvn clean install it builds successfully but under eclipse (RAD 9.5) my source files could not be compiled because of missing jars. Now add the maven-compiler-plugin:
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<includes>
<include>${some.dir}/libs/*.jar</include>
</includes>
</configuration>
</plugin>
Nevertheless those jars are not added under Maven Dependencies in the project structure.