Multi module maven project not being deployed on OpenShift (S2I) - java

I'm working on a maven multi module application, which consists of two modules:
Common
Webapp
My project structure is as follow:
-(root)pom
-Common
-Webapp
We're using openshift web console with S2I (source to image) deploy. The image that we choose is Jboss Eap. After providing git repository, Openshift starts to create required resources. It successfully compile and install our modules using maven, however it does not deploy it on standalone folder for Jboss. Looking at the build log, we can check all dependencies being retrieve and BUILD SUCCESS at end of log. But no artifact deployed on image jboss folder. We can confirm this either by looking at the log or using console to check pod files.
This project is on bitbucket
Root 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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test.parent</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>:: Parent ::</name>
<description>Parent POM for some app</description>
<modules>
<module>Common</module>
<module>Webapp</module>
</modules>
</project>
Common 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>
<parent>
<groupId>com.test.parent</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>com.test.common</groupId>
<artifactId>Common</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Common module</name>
<description>Module for common elements that exist between projects</description>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<!-- Compiler plugin enforces Java 1.8 compatibility and activates annotation
processors -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
...
</dependencies>
</project>
And finally, the web 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>
<parent>
<groupId>com.test.parent</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>com.test.external</groupId>
<artifactId>Webapp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name> Web</name>
<description>web module</description>
<properties>
<!-- Explicitly declaring the source encoding eliminates the following
message: -->
<!-- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered
resources, i.e. build is platform dependent! -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- JBoss dependency versions -->
<version.wildfly.maven.plugin>1.0.2.Final</version.wildfly.maven.plugin>
<version.jboss.spec.javaee.7.0>1.0.3.Final</version.jboss.spec.javaee.7.0>
<!-- other plug-in versions -->
<version.war.plugin>2.1.1</version.war.plugin>
<!-- maven-compiler-plugin -->
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<build>
<!-- Set the name of the WAR, used as the context root when the app
is deployed -->
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>${version.war.plugin}</version>
<configuration>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
...
<dependency>
<groupId>com.test.common</groupId>
<artifactId>Common</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>jar</type>
</dependency>
</dependencies>
</project>
Does anyone manage to achieve this?

There is a better way I think, which is to use the openshift profile in your web pom. This goes back to the way it was done in V2. In the openshift profile, which will get invoked during the build, copy the .war file into the deployment directory. The only difference I noticed was the deployments directory was named target instead of webapps, but I didn't do any detailed test since target seems to work. E.g.:
<profiles>
<profile>
<!-- When built in OpenShift the openshift profile will be used when invoking mvn. -->
<!-- Use this profile for any OpenShift specific customization your app will need. -->
<!-- By default that is to put the resulting archive into the deployments folder. -->
<!-- http://maven.apache.org/guides/mini/guide-building-for-different environments.html -->
<id>openshift</id>
<build>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<outputDirectory>target</outputDirectory>
<warName>ROOT</warName>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

I end up using maven-antrun-plugin to copy war into jboss standalone folder.
<profile>
<id>openshift</id>
<properties>
<axis2.repo.path>/opt/eap/custom_modules</axis2.repo.path>
<ssl.cacerts.java.path>${java.home}/lib/security/cacerts</ssl.cacerts.java.path>
</properties>
<build>
<resources>
<resource>
<directory>/src/webapp/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<copy todir="${jboss.deploy.path}">
<fileset dir="${compiled.war.location}"/>
</copy>
<echo>war copied to ${jboss.deploy.path}</echo>
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>

Related

Parent pom maven package not copying subfolder Quarkus microservice JAR files

Hello currently I have a parent git repo that houses two separate microservices written in Quarkus. For the ease of use within a CI/CD in the parent root folder I created a pom.xml that when I run a mvn package I want to be able to copy it into the parent/target/ path.
So the folder structure for instance is like this currently.
parent
target
pom.xml
/microservice1
- microservice1.jar
/microservice2
- microservice2.jar
but I want it to look like
parent
target
- microservice1.jar
- microservice2.jar
pom.xml
/microservice1
/microservice2
Since the parent folder/git repo doesn't have a pom.xml I generated one and this is what it looks like and I tried to use the maven-dependency-plugin suggestion that I found here
<?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.example</groupId>
<artifactId>parent-service</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>microservice1</module>
<module>microservice2</module>
</modules>
<name>parent-service</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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-artifact</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<type>${project.packaging}</type>
</artifactItem>
</artifactItems>
<!--<outputDirectory>../Main/target/dependencies</outputDirectory>-->
<!--CHANGE IS HERE -->
<outputDirectory>target</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
After running a mvn package at the parent folder the individual microservices are still packaging their own respective jars into microservice/target/ folders. Another weird interaction seems to be that the parent will just package it's own pom into it's own target/ folder. I could post the sub-microservices pom.xml files but those are quite long. What am I doing wrong? Did the way I create the pom not work well?

How to launch Spring Boot app from another Maven module?

I got a project which consists of two modules.
The first one is Spring Boot REST Web Service
And the second one is the code that should work with this service.
The issue: I need to start Web Service from another module from the code.
Of course, the best option here is to deploy Service to some remote host, but what are the options if I want to launch Service on the local machine?
The first idea was packaging Service module, then copying jar to the second module using maven-dependency-plugin and launching it as:
Runtime.getRuntime().exec("java -jar my-rest-service.jar");
Can I start Spring Boot app right from another module? Call Application.main() method or something?
You can build and install first module into your local maven repository (.m2 folder by default) as the jar library. Then you can use this library in your second module as the maven dependency.
After that you can start your application (second module) as usually spring-boot starts - with the main method.
Example:
First module - library:
<?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>
<url>https://example.com/module1</url>
<groupId>com.example</groupId>
<artifactId>module1</artifactId>
<name>module1</name>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<java.version>12</java.version>
</properties>
<build>
<finalName>module1</finalName>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Second module - application:
<?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>
<url>https://example.com/module2</url>
<groupId>com.example</groupId>
<artifactId>module2</artifactId>
<name>module2</name>
<version>1.0.0</version>
<packaging>jar</packaging>
<!--packaging>war</packaging-->
<properties>
<java.version>12</java.version>
</properties>
<build>
<finalName>module2</finalName>
<defaultGoal>package</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- com.example -->
<dependency>
<groupId>com.example</groupId>
<artifactId>module1</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
The Spring Boot Maven plugins package our application as executable JARs – such a file can't be used in another project since class files are put into BOOT-INF/classes.
In order to share classes with another project, the best approach to take is to create a separate jar containing shared classes, then make it a dependency of all modules that rely on them.
So in your spring boot module, you need to add classifier like:
...
<build>
...
<plugins>
...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<classifier>exec</classifier>
</configuration>
</plugin>
</plugins>
</build>
and of course, you need to add your spring boot module dependency in your pom where you want to use then you should be able to use Application.java.
Please check here: Using a Spring Boot Application as a Dependency.

How to use maven's repo as my class path when running java -cp

I am running a java application in dir /opt/myApp
And I have all needed jars in /opt/myApp/lib
The cmd I am using is java -cp /opt/myApp/lib/* org.my.App
But every time I re-compiled my codes(a maven project), I need to copy all lib files from workspace to /opt/myApp/lib.
I am wondering if it's possible to just use maven's repo as my class path to avoid copying all jars manually ?
My progress so far :
I also have some config files in /opt/myApp/config so I can't run it under maven project directly.
Maybe I can make /opt/myApp a maven project and do something in the pom.xml so I can run the app through maven ?
I have figured out a solution myself:
create a pom.xml in /opt/myApp like :
<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>my</groupId>
<artifactId>my-App-env</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
<properties>
<org.springframework-version>4.3.5.RELEASE</org.springframework-version>
</properties>
<name>Maven Quick Start Archetype</name>
<url>http://maven.apache.org</url>
<!-- repositories> <repository> <id>twitter-twttr</id> <url>http://maven.twttr.com/</url>
</repository> </repositories -->
<dependencies>
<dependency>
<groupId>my</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0</version>
<type>test-jar</type>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>config</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>test</id>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>org.my.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
then I can run the application through
mvn exec:java
and the exec-maven-plugin will generate classpath automatically.

Maven: How to install my jar to local repo after building

I want to build a package and install it to my local repo. My pom file is:
<?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.me</groupId>
<artifactId>MyApp</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<!-- This block declare a dependencies for this project -->
<dependencies>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<!-- This block declare a plugins -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
</plugin>
<!-- Installing to local repo -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<configuration>
<groupId>com.me</groupId>
<artifactId>MyApp</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<file>/Users/me/MyApp/target/MyApp-1.0.0.jar</file>
<generatePom>true</generatePom>
</configuration>
<executions>
<execution>
<id>install-jar-lib</id>
<goals>
<goal>install-file</goal>
</goals>
<phase>validate</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
But when I run "mvn package" comand I get a error that there is no file: /Users/me/MyApp/target/MyApp-1.0.0.jar So I comment install plugin, run "mvn package", uncoment install plugin and run "mvn package". Could I do this thing in one step? Without this comment-uncomment thing?
It's not clear why you need to invoke the install-file goal of maven-install-plugin but the problem lies in the phase. You configured it with <phase>validate</phase> when it should be <phase>package</phase> instead.
Take a look at the Introduction to the Build Lifecycle: phase validate is the first phase that is executed by Maven. At this time, the jar was not built so it can't work.
If you really want the artifact to be installed on the package phase (when running mvn package) then the phase should be set to package.
Note that instead of running mvn package, you should just call mvn install without the need of configuring the maven-install-plugin. The artifact will be automatically installed in your local repo with this command.
from what i know, mvn clean install, put the package in the local repo, without any need of any plugin

Maven web module not running from inside Eclipse

I have configure Maven Multimodule project and when i am doing
mvn clean install
It is Creating War and Jar files for both the modules in the parent project and when i am deploying war file in Tomcat6 it is working fine.
But when i am trying to run the web module from inside eclipse .
Right Click on project->Run on Server ->Selected Tomcat as a Server
Then Project not working .
The Web Module is depended on the java project which is also a part of Multimodule project so i added dependency of this(Java) project into my web project but in web project Java build path not containing this dependency .Can anyone know how can i resolve this issue with eclipse?
As i saw in eclipse's
workspace_maven.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\web\WEB-INF\lib
i did not found the Java project here .But when i am doing
mvn clean install
and deploying that war in Tomcat the /WEB-INF/lib directory containing my Java Project jar file.
My Web Module Pom.xml file...
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.test</groupId>
<artifactId>demo_parent</artifactId>
<version>1.0.0</version>
</parent>
<groupId>org.csdc</groupId>
<artifactId>demo-web</artifactId>
<version>7.0.0</version>
<packaging>war</packaging>
<name> web Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.test</groupId>
<artifactId>core-java</artifactId>
<version>5.0.0</version>
</dependency>
</dependencies>
<build>
<finalName>amanda-web</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
<wtpversion>2.0</wtpversion>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>2.0</version>
</plugin>
</plugins>
</build>
</project>
And my parent pom.xml 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>org.test</groupId>
<artifactId>demo_parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<modules>
<module>demo-web</module>
<module>core-java</module>
</modules>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<properties>
<maven.compiler.source>1.5</maven.compiler.source>
<maven.compiler.target>1.5</maven.compiler.target>
</properties>
</project>
*Note:-*I have removed repository and dependency from parent pom.xml to make pom short.
You should need the apache maven plugin
The goal should be like tomcat:run
It will download the new instance of tomcat in your target folder and deploy your war on that tomcat.
You can set all the setting for that tomcat or can refer the setting XML in the configuration tag of the plugin.
You can run it as debug mode and all the changes can be seen on the fly. (Java, JSP files only. no XML and properties)
for your help I am adding a code that would help you in running the tomcat server and deploy the WAR when you install the web module.
The application will be installed in tomcat 5 and on the root context.
You can access it on URL: http://localhost:8080/
<project>
...
<build>
<finalName>myWAR</finalName>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<configuration>
<path>/</path>
</configuration>
<executions>
<execution>
<id>RUN_TOMCAT</id>
<phase>install</phase>
<goals>
<goal>run</goal>
</goals>
<inherited>false</inherited>
<configuration>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

Categories

Resources