How to build p2 repository with p2-maven-plugin and use local directory as a source of JARs? - maven

Situation: Eclipse RCP application using Maven Tycho plugin, now I want to integrate properly p2-maven-plugin`. I have a directory containing all 3rd party JARs and Eclipse plugins as JARs.
How to use p2-maven-plugin to create p2 repository which includes all files from this directory?
<?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>my.company</groupId>
<artifactId>p2-site</artifactId>
<packaging>pom</packaging>
<version>1.0.0</version>
<build>
<plugins>
<plugin>
<groupId>org.reficio</groupId>
<artifactId>p2-maven-plugin</artifactId>
<version>1.3.0</version>
<executions>
<execution>
<id>default-cli</id>
<!-- How can I get there artifacts located in directory on my local machine??? These libs (in example below) are taken from .m2 maven repo -->
<configuration>
<artifacts>
<artifact>
<id>commons-lang:commons-lang:2.4</id>
</artifact>
<artifact>
<id>commons-lang:commons-lang:2.5</id>
</artifact>
<artifact>
<id>commons-lang:commons-lang:2.6</id>
</artifact>
<artifact>
<id>junit:junit:3.8.2</id>
</artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.5.v20120716</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webAppSourceDirectory>${project.basedir}/target/repository/</webAppSourceDirectory>
<webApp>
<contextPath>/site</contextPath>
</webApp>
<stopKey/>
<stopPort/>
</configuration>
</plugin>
</plugins>
</build>
...
Thanks!
EDIT:
Folder with plugins (JARs) from which I want to generate p2 repository.
usage of tycho extras p2 plugin in my master pom - doesn't work, I think maybe this piece of code is not called at all... How can I it call?

Related

Error running stand-alone jar with ojdbc jar dependency

I have the following 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>org.example</groupId>
<artifactId>do_can_proj</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc11-production</artifactId>
<version>21.1.0.0</version>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.do.can.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Running,
$ mvn clean package assembly:single
generates a jar in the target folder.
When I run the jar file from the command line:
java -jar target/do_can_proj-1.0-SNAPSHOT-jar-with-dependencies.jar
I get the following error:
SQL State: 08001
No suitable driver found for jdbc:oracle:thin:#hostname:1521:SID
And this is how I have my project setup:
When running with the -jar argument, the classpath is set to the entry in the manifest in the jar file. By default this is empty. It can be argued that the assembly target should be improved to allow this.
It needs to be set to include all libraries used by your code for this to work and the libraries needs to be added from the Maven repository. How exactly to do this depends on your needs. See How can I create an executable JAR with dependencies using Maven? for suggestions.

Maven to include JAR that is generated by that build

Normally we use dependency tag in pom.xml to have Maven include a JAR on the classpath and it also packages that dependency.
What if the JAR is generated in a step of the same project's build? I mean, it's not the compile plugin that generates it, it is a JAR without any java source and an external executable creates it.
I can use maven exec plugin to have my JAR generated and maven install plugin to have it installed to my local repository. But still I can't have it as a dependency in the same project: No matter which phase I put my JAR generator command in, the dependency check will happen before that and fail because the JAR does not yet exist.
Is system scope dependency my best choice? Then I need to give up packaging. And it's deprecated. And the JAR needs to be outside the project directory.
Or the JAR generator must be in a separate pom? Also not very nice because the JAR is only used by this one project.
Can I configure the dependency plugin to defer the dependency check and download to compile phase?
Any other solution?
This pom almost works, but first time I need to install the generated thing manually to the repo.
<?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>something-parent</artifactId>
<groupId>something</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>something-sample</artifactId>
<properties>
<jnbridge.path>C:/Program Files (x86)/JNBridge/JNBridgePro v9.0</jnbridge.path>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>generate-proxies-jar</id>
<phase>generate-test-resources</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>jnbproxy.bat</executable>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<phase>process-test-resources</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>jnbridge.local</groupId>
<artifactId>proxies</artifactId>
<version>0.0.0</version>
<packaging>jar</packaging>
<file>${basedir}/proxies.jar</file>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!--
Yes, these are true system-scope dependencies. JNBridge is expected to be
installed on the system wherever this project is built.
-->
<dependency>
<groupId>com.jnbridge.org.apache</groupId>
<artifactId>bcel</artifactId>
<version>5.1</version>
<scope>system</scope>
<systemPath>${jnbridge.path}/jnbcore/bcel-5.1-jnbridge.jar</systemPath>
</dependency>
<dependency>
<groupId>com.jnbridge</groupId>
<artifactId>jnbcore</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${jnbridge.path}/jnbcore/jnbcore.jar</systemPath>
</dependency>
<!--
This one will be installed to local maven repo in the process-test-resources phase,
as defined in the project/build/plugins section of this file.
-->
<dependency>
<groupId>jnbridge.local</groupId>
<artifactId>proxies</artifactId>
<version>0.0.0</version>
</dependency>
<!-- other dependencies... -->
</dependencies>
</project>

Maven - attach artifact with Maven descriptor files

For performance reasons I need to create a new EAR artifact beside a WAR archive without using multiple modules projekt. The EAR will contain the WAR (in the /web sub-directory) and the static application.xml file.
All I need to generate in the non-static way is Java classes and Maven descriptor files (META-INF/maven/${groupId}/${artifactId}/pom.xml and META-INF/maven/${groupId}/${artifactId}/pom.properties).
With classes there is no big deal, but how to generate and attach the Maven descriptor files to the new EAR?
This works well, but the Maven descriptor files are not generated inside the EAR archive.
...
<packaging>war</packaging>
...
<properties>
<compile.dir>${project.build.directory}/${project.build.finalName}</compile.dir>
<ear.zip>${project.build.directory}/my-archive.ear</ear.zip>
</properties>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>create-EAR-archive</id>
<phase>prepare-package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<!-- copy webapp files -->
<copy todir="${compile.dir}/web">
<fileset dir="src/main/webapp" />
</copy>
<!-- copy classes -->
<copy todir="${compile.dir}/web/WEB-INF/classes">
<fileset dir="${project.build.directory}/classes" />
</copy>
<!-- copy application.xml -->
<copy todir="${compile.dir}">
<fileset dir="src/main/resources" includes="META-INF/application.xml" />
</copy>
<!-- copy some big files -->
...
<!-- zip EAR -->
<zip destfile="${ear.zip}" basedir="${compile.dir}" />
</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.10</version>
<executions>
<execution>
<id>attach-EAR-artifact</id>
<goals>
<goal>attach-artifact</goal>
</goals>
<phase>package</phase>
<configuration>
<artifacts>
<artifact>
<file>${ear.zip}</file>
<type>ear</type>
<classifier>exploded</classifier>
</artifact>
</artifacts>
<archive>
<addMavenDescriptor>true</addMavenDescriptor>
</archive>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
The easiest thing is to have a multi module project.
A parent like :
<?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.greg</groupId>
<artifactId>ear-example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<myproject.version>1.0-SNAPSHOT</myproject.version>
</properties>
<name>ear-example</name>
<modules>
<module>example-ear</module>
<module>example-war</module>
</modules>
</project>
A war as you have, and an ear project:
<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>com.greg</groupId>
<artifactId>ear-example</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>example-ear</artifactId>
<packaging>ear</packaging>
<dependencies>
<dependency>
<groupId>com.greg</groupId>
<artifactId>example-jar</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.10.1</version>
<configuration>
</configuration>
</plugin>
</plugins>
</build>
</project>
Then just add any files you need to the src/main/application directory in the ear project and they will appear in the top level of your ear file.
See http://maven.apache.org/plugins/maven-ear-plugin/usage.html
The solution is to move the war package phase into another phase before the prepare-package phase, then there is a complete WAR archive containing the maven descriptor files. So it means, all the data are ready when the EAR archive is prepared.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>war</goal>
</goals>
</execution>
</executions>
</plugin>
The archive unzip and use in the prepare-package phase as before:
<!-- unzip the war -->
<unzip src="${project.build.directory}/${project.build.finalName}.war" dest="${project.build.directory}/war-exploded" />
<!-- move the war content -->
<move todir="${compile.dir}/web">
<fileset dir="${project.build.directory}/war-exploded" />
</move>

Referring dependencies that don't yet exist in Maven

I need to install a whole bunch of 3rd party jar files in my local repository, using maven-install plugin. That part is done with the following 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.sap</groupId>
<artifactId>sdk</artifactId>
<version>${sap.version}</version>
<build>
<plugins>
<plugin>
<groupId>com.soebes.maven.plugins</groupId>
<artifactId>iterator-maven-plugin</artifactId>
<version>0.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>iterator</goal>
</goals>
<configuration>
<items>
<item>cecore</item>
<item>cesession</item>
<item>celib</item>
</items>
<pluginExecutors>
<pluginExecutor>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.4</version>
</plugin>
<goal>install-file</goal>
<configuration>
<file>${bo.lib.dir}/#item#.jar</file>
<groupId>${sap.group}</groupId>
<artifactId>#item#</artifactId>
<version>${sap.version}</version>
<packaging>jar</packaging>
</configuration>
</pluginExecutor>
</pluginExecutors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<bo.lib.dir>C:\Program Files\SAP BusinessObjects\SAP BusinessObjects Enterprise XI 4.0\java\lib</bo.lib.dir>
<sap.group>com.sap</sap.group>
<sap.artifact>sdk</sap.artifact>
<sap.version>4.1</sap.version>
</properties>
<packaging>pom</packaging>
In a nutshell, I'm looping through these three items (cecore,celib, and cesession) to perform the actual installation.
I would like to then add these three items as dependencies, but since these won't exist before the package phase, Maven complains about that.
Ideally, I would like to instruct Maven to resolve the dependencies after the packaging, or to instruct Maven to trust that the dependencies will in fact become available.
Any ideas/suggestions?
Thanks!
Eric

Use jarsign plugin with assembly plugin in Maven 3

I'm using assembly plugin to package a list of applets into zip in one of modules with my maven project. here is the pom.xml:
<?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>
<packaging>pom</packaging>
<artifactId>applets-deploy</artifactId>
<name>deploy</name>
<dependencies>
<dependency>
<groupId>com.activx.lims</groupId>
<artifactId>applets-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.activx.lims</groupId>
<artifactId>ceplot-applet</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
......
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/assembly/resources.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
what I need is to also sign jars before they are packaged, can I use jarsign plugin here, and how? I can't find where the jar files are temporarily stored during the build process.
Thanks,
This would be a pretty normal use of the jarsigner plugin. By default, jars are built by the jar plugin during the package phase and output to the ${project.build.directory}, which defaults to target.
You'd just need to sign the jars some time after they're built during package and before you assemble the zip. You could do that by binding the assembly plugin to a later phase or by adding the jarsigner plugin above the assembly plugin and binding it to the package phase, too.

Resources