Maven - generate an RPM and Jar using a single POM file - java

I am working on Maven RPM Plugin and I have my directory structure as below
dir1
dir2
dir3
src
pom.xml
dir1,dir2,dir3 directories contain my custom files and the src directory has my Java source code.
I want to create the Jar file by building the Java application and then bundle everything to an RPM(dir1,dir2,dir3,src, jar file generated). I have tried the below
<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>xyz</groupId>
<artifactId>abc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>xyz</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<build.number>1</build.number>
<maven.rpm.plugin.version>2.0.1</maven.rpm.plugin.version>
</properties>
<profiles>
<profile>
<id>rpm</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>rpm-maven-plugin</artifactId>
<version>${maven.rpm.plugin.version}</version>
<executions>
<execution>
<id>generate-rpm</id>
<goals>
<goal>rpm</goal>
</goals>
</execution>
</executions>
<configuration>
<name>${project.artifactId}</name>
<copyright>XYZ</copyright>
<release>${build.number}</release>
<group>DOC/COD</group>
<mappings>
<mapping>
<directory>/tmp</directory>
</mapping>
</mappings>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
</repositories>
</project>
The target directory is being created with the JAR file of the Java source code but the RPM is not getting created. It throws the below error
Failed to execute goal org.codehaus.mojo:rpm-maven-plugin:2.0.1:rpm (generate-rpm) on project xyz: RPM query for default vendor returned: '127' executing '/bin/sh -c rpm -E '%{_host_vendor}'' -> [Help 1]
Please let me know if I am in the right direction and how to proceed on this.

Maven works best when conventions are followed, and when they are, very little configuration is needed. That being said, the convention of Maven is one artifact per module (and of course on pom.xml per module).
Therefore, the best way to structure your project would be like the following:
ProjectName
|
|--ChildJarModule
| |
| |--pom.xml
|
|--ChildRPMModule
| |
| |--pom.xml
|
|--pom.xml
If you do this, you'll have an easier time generating your RPM from the pom.xml in the ChildRPMModule (and you may very well solve your problem).

Related

import own .jar file as dependency via Maven and have install discover and retrieve it's dependencies

So this is probably a stupid question, but I have created an own library "testlib" which I want to include in other of my own Maven projects.
This is my own librarys .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.test.testlib</groupId>
<artifactId>testlib</artifactId>
<version>1.0-SNAPSHOT</version>
<name>${project.groupId}:${project.artifactId}</name>
<description>testlibrary</description>
<packaging>jar</packaging>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
In order to include my library in another new project, I have created a local repository in my new projects .pom
...
<repositories>
<repository>
<id>data-local</id>
<name>data</name>
<url>file://${project.basedir}/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.test.testlib</groupId>
<artifactId>testlib</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
...
Installing my own library "testlib" via mvn install is working fine for testlib itself. The problem is, that Maven will not detect that "testlib" requires org.slf4j, hence won't get it when I run install.
I have checked other dependencies in my .m2 folder and saw they have a .pom file with the same name as the .jar (for log4j that'd be log4j-1.2.17.pom). I tried copying testlibs .pom next to its .jar and changed the name accordinly, but that doesn't do it.
What do I have to do in order to get the same functionality as any other library from maven central? In other words, I don't want a fat .jar that has all dependencies included. I want a Maven project that adds my library as a dependency to discover that it needs sl4j and include it when mvn install is run.

Conditional inclusion of JARs in a WAR built by Maven

I have to build a war file using maven to include jars conditionally,
each jar is created by a seperate maven project which deploys jar to nexus(our organisations remote) repository
Eg : I have jars like these core.jar,reward.jar,payment.jar,domains.jar so on
I need to build a final war based on conditions(environmnet) to include above jars
Combination of final war(w1)
w1.war : core.jar,domains.jar
w1.war : core.jar,domains.jar,rewards.jar(Any way to specify to include this jar if rewards is applicable)
The Maven WAR Plugin allows you to include/exclude JARs. For example:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<packagingExcludes>
WEB-INF/lib/excluded.jar
</packagingExcludes>
<packagingIncludes>
WEB-INF/lib/included.jar
</packagingIncludes>
</configuration>
</plugin>
You can associate the inclusions/exclusions with a condition by using profiles. For example, let the WAR plugin use properties (${excludedResources}, ${includedResources}) ...
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<packagingExcludes>
${excludedResources}
</packagingExcludes>
<packagingIncludes>
${includedResources}
</packagingIncludes>
</configuration>
</plugin>
... and define values for those properties via profiles:
<profiles>
<profile>
<id>prod</id>
<properties>
<excludedResources>WEB-INF/lib/a.jar,WEB-INF/lib/b.jar</excludedResources>
<includedResources>WEB-INF/lib/c.jar</includedResources>
</properties>
</profile>
<profile>
<id>tst</id>
<properties>
<excludedResources>WEB-INF/lib/x.jar,WEB-INF/lib/y.jar</excludedResources>
<includedResources>WEB-INF/lib/z.jar</includedResources>
</properties>
</profile>
</profiles>
So, you can use the Maven WAR Plugin's built-in ability to tweak the WAR contents and you can make these tweaks conditional by using Maven profiles.
You can try to use profiles capabilities in maven. Each dependency can be included into its own profile block e.g. domains will be included only if you switch this profile on and services - by services profile.
At the same time you can identify common jars through the common dependency block (in our case core.jar will be common)
<?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>
<groupId>com.stackoverflow</groupId>
<artifactId>conditional-war</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>War Which Includes Jar By Conditions</name>
<!-- Common dependency block which will be always included -->
<dependencies>
<dependency>
<groupId>com.stackoverflow</groupId>
<artifactId>core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<profiles>
<!-- Profile for domains jars. Will be included by
profile\condition "domains" -->
<profile>
<id>domains</id>
<activation>
<property>
<name>domains</name>
</property>
</activation>
<dependencies>
<dependency>
<groupId>com.stackoverflow</groupId>
<artifactId>domains</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</profile>
<!-- Profile for domains jars. Will be included by
profile\condition "services" -->
<profile>
<id>services</id>
<activation>
<property>
<name>services</name>
</property>
</activation>
<dependencies>
<dependency>
<groupId>com.stackoverflow</groupId>
<artifactId>services</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
Command line for the activation can be following:
By this command line will be included core.jar and domains.jar
mvn clean install -Pdomains
In such case war will include core.jar and services.jar
mvn clean install -Pservices
And finally by this command line will be included all the jars
mvn clean install -Pdomains,services

org.codehaus.cargo.container.ContainerException: Cannot create deployable

In maven pom file, my project packaging type is "jar" like bellow.
<packaging>jar</packaging>
My cargo-maven2-plugin configuration in pom.xml file from the legacy code. I try to run it Eclipse Kelpler, but since the plugin configuration didn't mention cargo-maven2-plugin version(I don't know actual version for this configuration), Eclipse try to get the most recent one which is 1.4.8. Based on the configuration, the Tomcat version looks like 6.0.14, but container id is 5x. Whole configuration seems doesn't right and I try to make it work. Any suggestions? The package type must jar and I can't change it.
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<configuration>
<wait>${cargo.wait}</wait>
<container>
<containerId>tomcat5x</containerId>
<zipUrlInstaller>
<url>
http://archive.apache.org/dist/tomcat/tomcat-6/v${tomcat.version}/bin/apache-tomcat-${tomcat.version}.zip
</url>
<installDir>${installDir}</installDir>
</zipUrlInstaller>
</container>
<configuration>
<home>${project.build.directory}/tomcat5x/container</home>
<properties>
<cargo.hostname>${cargo.host}</cargo.hostname>
<cargo.servlet.port>${cargo.port}</cargo.servlet.port>
</properties>
<deployables>
<deployable>
<properties>
<context>ROOT</context>
</properties>
</deployable>
</deployables>
</configuration>
</configuration>
<executions>
<execution>
<id>start-container</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-container</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<properties>
<cargo.host>localhost</cargo.host>
<cargo.port>25888</cargo.port>
<cargo.wait>false</cargo.wait>
<tomcat.version>6.0.14</tomcat.version>
</properties>
I set type for to "jar" to match project. But when I run maven build in Eclipse Kelper, I am getting following error message. As you can see there is no allowed type "jar" is listed. Could any one help?
org.codehaus.cargo.container.ContainerException: Cannot create deployable. There's no registered deployable for the parameters (container [id = [default]], deployable type [jar]). Valid types for this deployable are:
- ear
- war
- rar
- bundle
- file
- sar
- ejb
According to Cargo's Tomcat 5.x doc only war files can be deployed to tomcat, that's why it is failing. Why don't you use war to create a webapp? I don't know your requirements, but usually if you deploy on Tomcat you have a webapp in a war file. What do you need to do? Do you have a servlet or jsp file in your project? Do you need it to use it as a library for an other webapp?
You could create a web app and include the jar generated by that project as a dependency. Use org.apache.marmotta:marmotta-archetype-webapp Maven archetype to create your project and add your legacy project dependency to the pom, it would be something 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/maven v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test.war</groupId>
<artifactId>test-war</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>test-war 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>
</dependencies>
<build>
<finalName>test-war</finalName>
</build>
<dependencies>
<dependency>
<groupId>legacyProjectGroupId</groupId>
<artifactId>legacyProjectArtifactId</artifactId>
<version>legacyProjectVersion</version>
</dependency>
</dependencies>
</project>

maven 3 interproject depedency with war packaging

I have Eclipse Indigo and M2E plugin installed.
So essentially I have a standard maven web project (let's call it proj-service) that is built into a war file in the package phase. This all works fine. My issue comes in when I have my other project (lets call it proj1) that needs to use classes from proj-service. I know that this is possible in maven+eclipse but it does not seem to be working at the moment. I have the following in proj1's pom right now:
<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.mycompany.foo</groupId>
<artifactId>proj1</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>proj1</name>
<properties>
<spring.version>3.1.0.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Maven Repo Libraries -->
.........
<!-- Interproject dependencies -->
<dependency>
<groupId>com.mycompany.foo</groupId>
<artifactId>proj-service</artifactId>
<version>1.0</version>
<type>war</type>
</dependency>
</dependencies>
<build>
<finalName>lsoap</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Unfortunately with Maven's war packaging you can't reuse classes from war project, because there is no direct build artifact you can use for the class path.
So, in order to do share classes properly you need to extract those common classes into a 3rd common project (jar packaging) and make it as dependency in both of your other projects.
First you have to change the configuration of your proj-service project in the way to change the configuration of the maven-war-plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<attachClasses>true</attachClasses>
<archiveClasses>true</archiveClasses>
...
</configuration>
</plugin>
This will it make possible to use the classes from the proj-service project in other projects via the following dependencies:
<dependency>
<groupId>myGroup</groupId>
<artifactId>myArtifact</artifactId>
<version>myVersion</myVersion>
<classifier>classes</classifier>
</dependency>
This will result in changing your dependency from:
<dependency>
<groupId>com.mycompany.foo</groupId>
<artifactId>proj-service</artifactId>
<version>1.0</version>
<type>war</type>
</dependency>
into:
<dependency>
<groupId>com.mycompany.foo</groupId>
<artifactId>proj-service</artifactId>
<version>1.0</version>
<classifier>classes</classifier/>
</dependency>

How to set up a minimal Maven pom.xml file for a Heroku worker process in Java?

First off: I'm not a Java coder. I'm new to the Java/Maven tool chain. We're using a Java library for a project which we want to launch as a Heroku background worker.
This project relies on two external libraries, the mongodb Java driver which is available through Maven's central repo, and another third party library. I've seen the Heroku article on "unmanaged dependencies", but something else appears missing, as I get an error like: Could not find the main class: com.company.myproject.MyApp Program will exit. when I try to run the app locally according to Heroku's instructions on "Getting Started with Java".
I noticed that their pom.xml file contains a Maven plugin maven-dependency-plugin to copy dependencies, and when I check my target/classes folder, I don't see any of the dependencies.
Heroku also publishes a guide on building background workers in Java. That pom.xml contains a build assembly plugin, which seems more complex.
I'm a bit lost in all this ceremony (especially coming from Rails), and I'd like to stat with the simplest possible pom.xml to get this running. Is there a Maven archetype file for Java workers on Heroku? I'm also using NetBeans as IDE, and it would be great to use the IDE tools for this, if available, but it's a secondary priority.
Below my pom.xml so far:
<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.myproject</groupId>
<artifactId>myproject</artifactId>
<version>0.1</version>
<packaging>jar</packaging>
<name>myproject</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.thirdparty</groupId>
<artifactId>thirdparty</artifactId>
<version>0.2.9</version>
<scope>provided</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>project-local</id>
<name>Project-local Repo</name>
<url>file:${project.basedir}/repo</url>
</repository>
</repositories>
</project>
You definitely need to use the maven-dependency-plugin to copy all of the dependencies into the target/dependency directory:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals><goal>copy-dependencies</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Then your Procfile needs to include those dependencies in the classpath:
foo: java -cp target/classes:target/dependency/* com.myproject.Main
Where com.myproject.Main is the class name of the Java class you want to run (which must contain a public static void main method. Note that this also adds the Java classes which are compiled from src/main/java into the target/classes dir.

Categories

Resources