I used the maven assembly package to create a single monolithic jar. Included is the datanucleus package, but I am getting errors because I did not maintain the OSGI structure (plugins.xml & META-INF/META-INF.MD). The answers on stackoverflow([question]: Datanucleus, JDO and executable jar - how to do it?) do not provide answers on how to create the single deployable jar.
Does anyone know what maven directives I can use to ensure OSGI structure?
So, the problem is that packing everything into a monolithic jar is that the OSGI structures get overritten by the OSGI structure of other jars. The benefit of the monolithic jar was to have a single file to push to the server but not necessary (since I was using capistrano to push to the server)
Instead, it's easier to just copy the jars into the final build directory. Heroku's Java setup has a perfect example of this:
https://devcenter.heroku.com/articles/java
<?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.example</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>helloworld</artifactId>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>7.6.0.v20120127</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
<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>
</project>
Related
I am trying to create a custom parameter less plugin by referring to create simple mojo creation instructions by apache in the below link.
https://maven.apache.org/guides/plugin/guide-java-plugin-development.html#
But I am unable to run the same using maven goal. Kindly suggest what I am missing. As I am new to maven and there aren't many tutorials on custom mavem plugin creation. Any suggestions on this topic will be helpful to me. Thanks in advance.
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>sample.plugin</groupId>
<artifactId>hello-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<name>Sample Parameter-less Maven Plugin</name>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.0</version>
</dependency>
<!-- dependencies to annotations -->
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>sample.plugin</groupId>
<artifactId>hello-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<goals>
<goal>sayhi</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
GreetingMojo.java
package sample.plugin.hello_maven_plugin;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Mojo;
/**
* Says "Hi" to the user.
*
*/
#Mojo( name = "sayhi")
public class GreetingMojo extends AbstractMojo
{
public void execute() throws MojoExecutionException
{
getLog().info( "Hello, world." );
System.out.println("My first maven plugin.");
}
}
I am trying to run the pom.xml using package command and mvn install command.
Generally you shouldn't execute the plugin goal in the plugin you are trying to build. The reason you cannot build the plugin with mvn install is because you have configured maven to expect the plugin to already be built.
This section belongs in a different POM file, not the plugin POM file.
<build>
<plugins>
<plugin>
<groupId>sample.plugin</groupId>
<artifactId>hello-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<executions>
<execution>
<goals>
<goal>sayhi</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
After you have removed this section from the plugin POM you can run mvn install to install your plugin locally. Once installed you can re-add the section to the POM if you really wanted to, but ideally you would add it to a completely different maven project and execute maven in that project.
This question already has answers here:
Differences between dependencyManagement and dependencies in Maven
(15 answers)
Closed 6 years ago.
I already create a jar that contain some common jars, which we will use it in different applications and can be used via many users.
To Solve this, i create a common jars, so i can control this commons jars and version of them, then store it in nexus server, so it can contain the parent pom and can be used by many developers.
So i create a simple maven java application and in the pom of this jar, i put for example :
Parent 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.emp</groupId>
<artifactId>dependencymanager</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>6.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
Then in the child applications i use this jar like this:
<dependencies>
<dependency>
<groupId>com.emp</groupId>
<artifactId>dependencymanager</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
But when i try to use org.primefaces, i can't. Where i'm wrong, or i'm wrong in the implementation of dependency management?
Note
I already read this : http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
So in the example of this tutorial it not specify the version of the parent pom, so when i delete it show me an error like this :
'dependencies.dependency.version' for com.emp:dependencymanager:jar is missing.
Question
How can i implement dependency management in this case?
Is there any better way then this to work with same dependencies with
many projects and many devellopers.
I use nexus to store my jars
Thank you.
In the parent pom, add the dependencies directly, leaving out the dependencyManagement tags. With the dependencyManagement you only specify that if such a dependency is declared in your pom, that the version specified in the parent pom is to be used. But your parent pom does not per se include those dependencies as transitive dependencies.
I have used Maven Shade plugin for having dependencies in the generated jar file. Check if similar code works for you
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Goal: Create an executable uber jar with maven shade plugin that can be executed during the mvn compile of another pom.
Repro steps:
Create a pom.xml for the "publisher" component using below pom.
Use a Jenkins build to mvn deploy it (mvn install will work as well)
Add dependency to pom.xml for "consumer" (pom below)
mvn compile the consumer
Expected behavior:
Uber jar for publisher is downloaded somewhere in consumer/target directories
Actual:
Uber jar does not appear in consumer directory
Component 1: Publisher
<?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.mec.experiment</groupId>
<artifactId>publisher</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.6.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestFile>src/main/resources/META-INF/MANIFEST.mf</manifestFile>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Component 2: Consumer
<?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.mec.experiment</groupId>
<artifactId>consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.mec.experiment</groupId>
<artifactId>publisher</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
The answer from the possible duplicate I linked to in the comments has a link to a dead example page. Here's a supplement for you. The plugin configuration would belong inside the consumer pom.
exec:java
Full name:
org.codehaus.mojo:exec-maven-plugin:1.5.0:java
Description:
Executes the supplied java class in the current VM with the enclosing project's dependencies as classpath.
Attributes:
Requires a Maven project to be executed.
Requires dependency resolution of artifacts in scope: test.
The goal is thread-safe and supports parallel builds.
Since version: 1.0.
See especially executableDependency for your use case. That looks like it will allow you to reference producer according to its group id and artifact instead of hard-coding a path.
There is a class in a maven dependency that is incompatible with Java 8.
How do you properly fix that problem?
Right now I'm doing the following:
Create a package with the same name
Create a class with the same name in that package
Copy and paste the code
Fix the incompatible API call
The problem is that this class contains API calls to restricted classes and although I changed the Eclipse compiler settings (Window -> Preferences -> Java -> Compiler -> Error/Warnings -> Deprecated and restricted API -> Forbidden reference (access rule): Error -> Warning) to allow access the project will only compile sometimes. If it doesn't compile I'll get a "can't find symbol" error.
Edit:
Here are the details you asked for:
Dependency: http://mvnrepository.com/artifact/com.sun.xml.wss/xws-security/3.0
Class: EncryptionProcessor
Necessary change:
// Change line 1053 FROM:
// _dataEncryptor = XMLCipher.getInstance(dataEncAlgo, _dataCipher);
// TO:
_dataEncryptor = XMLCipher.getInstance(dataEncAlgo);
Edit-2:
Maven build error:
[ERROR] symbol: class XMLCipher
[ERROR] location: class com.sun.xml.wss.impl.apachecrypto.EncryptionProcessor
[ERROR] /C:/Users/{name}/development/eclipse_workspace/git/xws-security/src/main/java/com/sun/xml/wss/impl/apachecrypto/EncryptionProcessor.java:[1482,98] cannot find symbol
Here is a detailed guide describing what I did exactly:
Create new Maven project in Eclipse
Configure Maven settings of new project (Important: Use the same group and artifact ID and only change the version number)
<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.sun.xml.wss</groupId>
<artifactId>xws-security</artifactId>
<version>3.0-java8-fix</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
</project>
Add dependency of bugged JAR
<dependencies>
<dependency>
<groupId>com.sun.xml.wss</groupId>
<artifactId>xws-security</artifactId>
<version>3.0</version>
<exclusions>
<exclusion>
<artifactId>xmldsig</artifactId>
<groupId>javax.xml.crypto</groupId>
</exclusion>
<exclusion>
<artifactId>activation</artifactId>
<groupId>javax.activation</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
Create Java file in the same package of class that needs to be fixed
package com.sun.xml.wss.impl.apachecrypto;
public class EncryptionProcessor {
// The FIX goes here
}
Add Maven shade build plug in to handle creation of patched JAR file (this is not the only plug in to handle this kind of task - e.g. dependency:unpack)
<build>
<plugins>
<!-- plug in for creation of patched JAR file -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>com.sun.xml.wss:xws-security:3.0</artifact>
<includes>
<include>**/*.class</include>
<include>**/*.xml</include>
</includes>
<excludes>
<exlude>
com/sun/xml/wss/impl/apachecrypto/EncryptionProcessor.class
</exlude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Include patched JAR in other projects as necessary (Note: If you experience ClassNotFoundExceptions or similar errors do this: Right-click on the project -> Properties -> Maven -> "Resolve dependencies from Workspace projects":false)
In case you are not familiar with Maven. Here is the complete pom.xml: http://pastebucket.com/88444
Similar to Steven S.'s answer, but using the maven-dependency-plugin. Based on this blog post.
I changed the name of the patched library (not the version), but it depends on your needs what works better for you.
The dependency on the original library should be marked as <optional>true</optional>. Otherwise, the projects that depend on your patched library will also depend on the original library, which means that both the patched and the original version will be on the classpath, which can lead to all kinds of problems.
If your project is a child project, you can still use a completely different groupId and version than your parent pom. Doesn't matter.
You can exclude the classes you patch from unpacking, but it's probably not necessary, because Maven will first unpack the original library and then compile your new version, which means that the original classes are overwritten. Nice!
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<!-- remove this if you don't have a parent pom -->
<parent>
<groupId>my.company</groupId>
<artifactId>my.company</artifactId>
<version>1.2.3</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>com.foo</groupId>
<artifactId>foo-bar-patched</artifactId>
<version>4.5.6</version>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.foo</groupId>
<artifactId>foo-bar</artifactId>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<!-- excludes are probably not necessary -->
<!-- <excludes>**/Foo.class,**/Bar.class</excludes> -->
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.foo</groupId>
<artifactId>foo-bar</artifactId>
<version>4.5.6</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>
General solution:
download all project sources
apply your modification
use version control so that change isn't lost
change version in pom.xml, for example from 3.0 to 3.0-patched
launch maven build
copy generated artifacts to you repository/Artifactory, if you use one
change dependency version in your own project
Im facing an issue how to correctly package my enterprise (EAR) application with simple WAR and EJB3 module for JBoss7 application server. The thing is, that EJB module is using XML-RPC library (from Apache) and Im still getting NoDefClassFound (classes from this xmlrpc lib) during deployment of EAR.
The thing is, that maven-ejb-plugin does not package dependencies within final EJB jar but maven-ear-plugin does package it at the root of EAR directory.
When EAR gets deployed, INSTALL is invoked on inner EJB module but it does not find xmlrpc lib class (it is not packaged with EJB jar but EAR and it does not have any entry in manifest).
EJB 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>
<groupId>cz.ctu.fee.voxport.app_logic</groupId>
<artifactId>core</artifactId>
<version>1.0</version>
<packaging>ejb</packaging>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.xmlrpc</groupId>
<artifactId>xmlrpc-common</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.xmlrpc</groupId>
<artifactId>xmlrpc-client</artifactId>
<version>3.1.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.3</version>
<configuration>
<ejbVersion>3.1</ejbVersion>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
Is there any way how to cleanly solve this using maven?
I managed to solve the problem. It seems that these libraries has to be packaged within /lib directory and not in root of EAR. Adding defaultLibBundleDir element solved the problem.
E.g.:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.6</version>
<configuration>
<defaultLibBundleDir>lib</defaultLibBundleDir>
...
Did you leave the <addClasspath>true</addClasspath> on the EJB config?
Well, you can leave it like this, but you'll get a heap of log entries (WARN) on server start complaining about the classpath entries.
I prefer to set it to false. <addClasspath>false</addClasspath>