Maven creates class files in wrong directory - maven

I just started learning Maven. I have two java files and they are shown below
I compiled it successfully using 'mvn compile' and able to see the class files
However, I expect 'target/classes/src2/SecProg.class' instead of 'target/classes/SecProg.class'
Output
./target/classes/SecProg.class
./target/classes/src1/FirstProg.class
Please let me know what am I doing wrong
My source tree looks like below
[sathish#oc3855733574 java_tools]$ tree
.
├── pom.xml
├── pom.xml_org
├── src1
│   └── FirstProg.java
├── src2
│   └── SecProg.java
My POM.xml looks like below
<project 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>java_tools</groupId>
<artifactId>first_prog</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<build>
<sourceDirectory>.</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<includes>
<include>src2/*.java</include>
<include>src1/*.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>

Related

Missing plugin and feature folders for an update site built with Maven Tycho

I am building an eclipse plugin with Tycho. I want to create an Update Site for it. I have the following components:
parent (pom)
the plugin (eclipse-plugin)
the feature (eclipse-feature)
the update site (eclipse-repository)
But when I run mvn clean package in the target folder of my update site project I have:
├── lorem-ipsum-eclipse-update-1.0.1-SNAPSHOT.zip
├── local-artifacts.properties
├── p2agent
│   ├── org.eclipse.equinox.p2.core
│   │   └── cache
│   │   └── artifacts.xml
│   └── org.eclipse.equinox.p2.engine
│   └── profileRegistry
├── p2artifacts.xml
├── p2content.xml
├── repository
│   ├── artifacts.jar
│   ├── artifacts.xml.xz
│   ├── content.jar
│   ├── content.xml.xz
│   └── p2.index
└── targetPlatformRepository
└── content.xml
As you can see the plugin and feature folders are missing in target/repository.
This is my parent pom:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.lorem.ipsum.eclipse</groupId>
<version>1.0.1-SNAPSHOT</version>
<artifactId>lorem-ipsum-eclipse-parent</artifactId>
<packaging>pom</packaging>
<properties>
<tycho-version>2.2.0</tycho-version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<modules>
<module>lorem-ipsum-eclipse-feature</module> <!-- packaging: eclipse-feature -->
<module>lorem-ipsum-eclipse-plugin</module> <!-- packaging: eclipse-plugin -->
<module>lorem-ipsum-eclipse-update</module> <!-- packaging: eclipse-repository -->
</modules>
<repositories>
<repository>
<id>2020-12</id>
<layout>p2</layout>
<url>http://download.eclipse.org/releases/2020-12</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>${tycho-version}</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-p2-repository-plugin</artifactId>
<version>${tycho-version}</version>
<configuration>
<includeAllDependencies>true</includeAllDependencies>
<createArtifactRepository>true</createArtifactRepository>
<compress>true</compress>
</configuration>
</plugin>
<!--Enable the replacement of the SNAPSHOT version in the final product configuration-->
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-packaging-plugin</artifactId>
<version>${tycho-version}</version>
<executions>
<execution>
<phase>package</phase>
<id>package-feature</id>
<configuration>
<finalName>${project.artifactId}_${unqualifiedVersion}.${buildQualifier}</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
What am I doing wrong?
Thanks in advance.
UPDATE: As howlger requested here is update site's category.xml.
<?xml version="1.0" encoding="UTF-8"?>
<site>
<feature id="com.lorem.ipsum.eclipse.feature" version="0.0.0">
<category name="com.lorem.ipsum.eclipse.category"/>
</feature>
<category-def name="com.lorem.ipsum.eclipse.category" label="Lorem Ipsum">
<description>
Contains features for Lorem Ipsum plugin
</description>
</category-def>
</site>
By the way my file is named site.xml because if I named category.xml I have this error when i tried to run any maven goal:
$ mvn clean
...
[ERROR] Cannot resolve project dependencies:
[ERROR] Software being installed: lorem-ipsum-eclipse-update raw:1.0.1.'SNAPSHOT'/format(n[.n=0;[.n=0;[-S]]]):1.0.1-SNAPSHOT
[ERROR] Missing requirement: lorem-ipsum-eclipse-update raw:1.0.1.'SNAPSHOT'/format(n[.n=0;[.n=0;[-S]]]):1.0.1-SNAPSHOT requires 'org.eclipse.equinox.p2.iu; com.lorem.ipsum.eclipse.feature.feature.group 0.0.0' but it could not be found
[ERROR]
[ERROR] See https://wiki.eclipse.org/Tycho/Dependency_Resolution_Troubleshooting for help.
[ERROR] Cannot resolve dependencies of MavenProject: com.globant.augmented.coding.eclipse:lorem-ipsum-eclipse-update:1.0.1-SNAPSHOT # /home/me/workspaces/java/lorem-ipsum-eclipse-project/lorem-ipsum-eclipse-update/pom.xml: See log for details -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MavenExecutionException
I followed the instructions of the book Eclipse 4 Plug-in Development by Example Beginners Guide by Dr Alex Blewitt which uses an old version of tycho (0.18.0) and I have used 2.2.0. Maybe in this version they fixed the fact of renaming site to category since in the same book they mentioned that it was a meaningless change.
I quote:
Rename the site.xml file to category.xml . (This is an entirely
pointless change required by p2 since the files are identical in
format.)
According the the error message (... lorem-ipsum-eclipse-update ... Missing requirement: ... com.lorem.ipsum.eclipse.feature.feature.group ...) the update site category.xml refers a missing feature.
Make sure to use the same feature ID (<feature id="...") in the following two files:
<your-feature>/feature.xml
<your-update-site>/category.xml
See also the vogella tutorial Eclipse Tycho for building Eclipse Plug-ins and RCP applications: in category.xml the feature is referenced via the ID com.vogella.tycho.feature.
com.vogella.tycho.feature

How to migrate a Maven based web application POM to use Gradle?

Yes, yes, I know! There are hundreds of Maven-to-Gradle related questions here on SO, but none of them, AFAICT addresses the inherent difficulties (of a constantly moving target [Gradle!]) of porting a very simple Maven POM to use Gradle for compilation.
In this particular case, we have an app the original developers are relying on using the GWT plugin for Eclipse, but that many other developers would like to use something more modern and less bloated, i.e. Gradle. All sources simply brush this off to, "you can simply convert pom.xml in Gradle", as is stated in the Gradle documentation, unfortunately this is simply not working for most practical purposes.
The project under consideration is circuitJS1 which was ported to use Maven here. The resulting pom.xml reads:
<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.lushprojects.circuitjs1</groupId>
<artifactId>circuitjs</artifactId>
<version>2.1.15</version>
<packaging>gwt-app</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt</artifactId>
<version>2.8.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- generating dependency report is slow; disable it -->
<dependency.locations.enabled>false</dependency.locations.enabled>
</properties>
<dependencies>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-user</artifactId>
</dependency>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt-dev</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId> <!-- artifact with sources is easier to handle during development -->
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- declaring only in order to skip during site deployment -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.7</version>
<executions>
<execution>
<id>deploy</id>
<phase>deploy</phase>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
<configuration>
<skip>true</skip>
<siteDirectory>site</siteDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.9</version>
<configuration><!-- we don't need those reports; disabling speeds up
build -->
<dependencyDetailsEnabled>false</dependencyDetailsEnabled>
<dependencyLocationsEnabled>false</dependencyLocationsEnabled>
</configuration>
</plugin>
<!-- gwt compiler -->
<plugin>
<groupId>net.ltgt.gwt.maven</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>1.0-rc-9</version>
<extensions>true</extensions>
<configuration>
<moduleName>com.lushprojects.circuitjs1.circuitjs1</moduleName>
<!-- this is the best setting for a laptop with 2 cores and HT -->
<localWorkers>0.5C</localWorkers>
<warName>circuitjs</warName>
<optimize>9</optimize>
<compilerArgs>
<compilerArg>-style</compilerArg>
<compilerArg>PRETTY</compilerArg>
</compilerArgs>
<codeServerPort>8888</codeServerPort>
</configuration>
</plugin>
<!-- copy a few things around before packaging the website -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-resources</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/site</outputDirectory>
<resources>
<resource>
<directory>war</directory>
</resource>
<resource>
<directory>${project.build.directory}/${project.name}-${project.version}/circuitjs1</directory>
<targetPath>circuitjs1</targetPath>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Cloning that repository and simply doing: mvn install will give you a working (circuit simulation) website, that you can open with your browser, under the file location:
file:///C:/path/to/circuitjs1/target/site/circuitjs.html
However, using gradle init --type pom to convert to Gradle, yield a build.gradle like this:
/* This file was generated by the Gradle 'init' task. */
plugins {
id 'java'
id 'maven-publish'
}
repositories {
mavenLocal()
maven {
url = 'http://repo.maven.apache.org/maven2'
}
}
dependencies {
compile 'com.google.gwt:gwt-user:2.8.2'
compile 'com.google.gwt:gwt-dev:2.8.2'
}
task sourcesJar(type: Jar) {
classifier = 'sources'
from(sourceSets.main.allJava)
}
group = 'com.lushprojects.circuitjs1'
version = '2.1.15'
sourceCompatibility = '1.8'
publishing {
publications {
maven(MavenPublication) {
from(components.java)
artifact(sourcesJar)
}
}
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
Unfortunately, using that with gradle build, doesn't seem to yield anything that can be ran in the browser. It does generate ./build/ directory but which contents are not of any use at all.
build
├── classes
│   └── java
│   └── main
│   └── com
├── generated
│   └── sources
│   └── annotationProcessor
│   └── java
├── libs
│   └── circuitjs-2.1.15.jar
└── tmp
├── compileJava
└── jar
└── MANIFEST.MF
I have also tried to beat this file into shape by adding things like:
//id 'com.gradle.build-scan' version '2.2.1'
id 'java'
id 'maven'
//id 'gwt-maven'
id 'maven-publish'
id 'war'
//id 'org.metahelicase.site-builder' version '1.1'
//id 'org.jbake.site' version '5.0.0'
//id 'gradle.site' version '0.6'
//id 'com.stehno.gradle.webpreview' version '0.3.0'
But to no avail...
How can I make this build.gradle script work correctly to produce the web files necessary to run the application locally in my browser?
Are there any up-to-date tools available that can do this for you?
The problem was that because the Maven build file (pom.xml) had so many plugins in it, that I was thinking they were needed also in Gradle. That is a big mistake because Gradle's build.gradle file only need minimal plugin info. The rest is downloaded and used automagically.
In my case it only needed the Gradle version of GWT, the gwt-gradle-plugin plugin.
The corrected and hugely simplified build.gradle becomes:
// This must be before plugins!
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.wisepersist:gwt-gradle-plugin:1.0.8"
}
}
plugins {
id 'java'
}
repositories {
mavenLocal()
maven {
url = 'https://plugins.gradle.org/m2/'
url = 'http://repo.maven.apache.org/maven2'
}
}
dependencies {
implementation 'com.google.gwt:gwt-user:2.8.2'
implementation 'com.google.gwt:gwt-dev:2.8.2'
}
apply plugin: 'gwt-compiler'
...
Now you can just run:
gradle compileGwt --console verbose --info
and all files will be built in the ./build directory.

Spring boot test multi module maven application

I have a multi-module maven application which uses Spring boot:
- spring boot parent
- myproject parent (both parent and module pom)
- module1
- module2
- module-it (integration tests)
In my module-it, I add the other modules as dependencies.
When I build my project with maven, I get "Build Success":
mvn clean install
So far so good.
Yet I would like each of my modules to be an executable jar at the end of the build. With the above settings, the manifest is not defined and the jar is not executable.
To fix this issue, I've added the following in my module1 and module2 pom files:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
With this setting, my jar file is executable but I cannot build anymore. Classes that I use in my module-it are not found.
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project module-it: Compilation failure: Compilation failure:
[ERROR] /home/user/<path-to-project>/testing/module-it/src/test/java/com/mycompany/testing/greeting/GreetingControllerIT.java:[20,17] cannot find symbol
[ERROR] symbol: class GreetingController
[ERROR] location: class com.mycompany.testing.greeting.GreetingControllerIT
[ERROR] /home/user/<path-to-project>/testing/module-it/src/test/java/com/mycompany/testing/hello/HelloControllerIT.java:[20,17] cannot find symbol
[ERROR] symbol: class HelloController
[ERROR] location: class com.mycompany.testing.hello.HelloControllerIT
[ERROR] /home/user/<path-to-project>/testing/module-it/src/test/java/com/mycompany/testing/greeting/GreetingControllerIT.java:[16,27] cannot find symbol
[ERROR] symbol: class GreetingController
[ERROR] /home/user/<path-to-project>/testing/module-it/src/test/java/com/mycompany/testing/hello/HelloControllerIT.java:[16,27] cannot find symbol
[ERROR] symbol: class HelloController
Can you please help me understand why spring-boot-maven-plugin makes my build fail and how I can solve the issue?
Thanks in advance for your help.
To solve this issue, we can add a classifier as described in the documentation custom repackage classifier
The plugin then becomes:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
Also you could set repackage goal parameter attach to false:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<attach>false</attach>
</configuration>
</execution>
</executions>
</plugin>
At least for the spring-boot-starter-parent:2.6.0 pom the configuration for the spring-boot-maven-plugin contains an <id>repackage</id> for the execution of the repackage goal
So I had to add the line <id>repackage</id> too.
Full configuration:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
Late answer to an old question but i've just been working on this for the past hours. Even though the previous answers were helpfull they don't explain why this problem is happening.
For others that may comes accross this issue here is a detailed explnation why it's not working
In a Nutshell
Usualy Maven package your application as a regular .jar with all compiled class being in a well known location in the .jar file.
So it's pretty straighforward for a compiler to import the .jar as a library and to load the available .class.
But the spring-boot-maven-plugin is actually modifying the .jar structure to leverage spring-boot logic when you start the .jar application.
In short, the .class are not available to be imported as a "library" from the resulting .jar, because the spring class have took the well known location for itself.
Detailed explanation
Let's explore the problem with an example
Project structure
Let's imagine a project with multiple maven modules like so
my-app/ -- The parent project
├─ pom.xml
├─ application/
│ ├─ pom.xml
├─ integration-tests/
│ ├─ pom.xml
Given the following pom.xml files:
my-app/pom.xml:
<project [...]>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0</version>
<relativePath/>
</parent>
<groupId>com.me</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>application</module>
<module>integration-tests</module>
</modules>
</project>
my-app/application/pom.xml:
<project [...]>
<parent>
<groupId>com.me</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>application</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
my-app/integration-tests/pom.xml:
<project [...]>
<parent>
<groupId>com.me</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>integration-tests</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.me</groupId>
<artifactId>application</artifactId>
</dependency>
</dependencies>
</project>
Why is it not building
Let's try to package our app
/my-app$ mvn package
Of course it will miserably fail with an error cannot find symbol, but why is it so?
Let's take a look at our architecture after the failed build:
my-app/ -- The parent project
├─ pom.xml
├─ application/
│ ├─ pom.xml
│ ├─ target/
│ │ ├─ application-1.0.0-SNAPSHOT.jar
│ │ ├─ application-1.0.0-SNAPSHOT.jar.original
├─ integration-tests/
│ ├─ pom.xml
│ ├─ target/
The spring-boot-maven-plugin has done several things to the application module output:
renamed the compiled application-1.0.0-SNAPSHOT.jar to application-1.0.0-SNAPSHOT.jar.original
created it's own .jar with the name of application-1.0.0-SNAPSHOT.jar
Let's explore the structure of the application-1.0.0-SNAPSHOT.jar:
BOOT-INF/
├─ classes/
│ ├─ com/
│ │ ├─ me/ -- The compiled .class of your project reside here
META-INF/
org/
├─ springframework/
│ ├─ boot/ -- contains the spring boot loader classes
As you can see the .class files at the root of your .jar are the spring boot loader classes, not our own .class that are relegated to the BOOT-INF/classes/ folder.
This is not conventional, and when the .jar is imported as a dependency it won't search here for class to import.
Because of that, when maven try to package the integration-tests module, it fails because the class present in the application-1.0.0-SNAPSHOT.jar are actually a bunch of spring class instead of the one you are trying to import from application module.
If you were to look at the structure of the application-1.0.0-SNAPSHOT.jar.origial it would be something like so:
META-INF/
com/
├─ me/ -- contains the spring boot loader classes
Solution
Getting rid of spring-boot-maven-plugin is not an acceptable solution; Of course your project will be buildable, but the resulting .jar won't be a spring boot standalone running .jar.
Instead you can instruct the spring-boot-maven-plugin to not replace the original jar and to build to spring boot jar with another name.
To do so you'll need to configure the spring-boot-maven-plugin in the application module:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>repackage</id>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Now when you build your project you'll have something like that:
my-app/ -- The parent project
├─ pom.xml
├─ application/
│ ├─ pom.xml
│ ├─ target/
│ │ ├─ application-1.0.0-SNAPSHOT.jar -- the original untouched .jar
│ │ ├─ application-1.0.0-SNAPSHOT-exec.jar -- the spring boot executable .jar
├─ integration-tests/
│ ├─ pom.xml
│ ├─ target/
│ │ ├─ integration-tests-1.0.0-SNAPSHOT.jar

maven-war-plugin ignores <archiveClasses>

The <archiveClasses> option has no effect.
Running mvn clean compile war:exploded produces a war directory with .class files in the classes directory, and they are not archived into a jar in the lib directory neither. war:war produces same result.
Plugin configuration:
...
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<archiveClasses>true</archiveClasses>
</configuration>
</plugin>
...
Workarounds?
Maven version 3.3.3, maven-war-plugin version 2.6.
JIRA ticket – https://issues.apache.org/jira/browse/MWAR-355
This is the project in question: https://bitbucket.org/dmos62/raudondvaris
The first thing is you should move the plain configuration into a pluginManagement block like this:
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<archiveClasses>true</archiveClasses>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
If you do the above the classes will be created within the war archive by using: mvn clean compile war:war
~/ws-git/so-questions/so-5 (master)$ unzip -t target/web-1.0.0-SNAPSHOT.war
Archive: target/web-1.0.0-SNAPSHOT.war
testing: META-INF/ OK
testing: META-INF/MANIFEST.MF OK
testing: WEB-INF/ OK
testing: WEB-INF/classes/ OK
testing: WEB-INF/lib/ OK
testing: WEB-INF/lib/commons-fileupload-1.1.1.jar OK
testing: WEB-INF/lib/commons-io-1.1.jar OK
testing: WEB-INF/lib/web-1.0.0-SNAPSHOT.jar OK
testing: WEB-INF/web.xml OK
testing: META-INF/maven/com.soebes.examples.so/web/pom.xml OK
testing: META-INF/maven/com.soebes.examples.so/web/pom.properties OK
testing: META-INF/INDEX.LIST OK
No errors detected in compressed data of target/web-1.0.0-SNAPSHOT.war.
This will also working for your call mvn clean compile war:exploded.
└── web-1.0.0-SNAPSHOT
├── META-INF
└── WEB-INF
├── classes
├── lib
│   ├── commons-fileupload-1.1.1.jar
│   ├── commons-io-1.1.jar
│   └── web-1.0.0-SNAPSHOT.jar
└── web.xml
The reason for this behaviour is simply cause by using a goal like war:war, or war:exploded there will be no life cycle started which means the configuration in the pom is not taken into account. If you like having a configuration for your command line calls you can do this by using a special configuration for command line calls like this (The id default-cli is the important part):
<project>
<build>
<plugins>
<plugin>
<groupId...>
<artifactId...>
<executions>
<execution>
<id>default-cli</id>
<configuration>
.....
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
which means having a special configuration for command line calls. Starting with Maven 3.3.1 it is possible having more than one configuration for command line calls by using it like:
<project...>
<build>
<plugins>
<plugin>
<groupId>...</groupId>
<artifactId>...</artifactId>
<executions>
<execution>
<id>first-cli</id>
<configuration>
....
</configuration>
</execution>
<execution>
<id>second-cli</id>
<configuration>
....
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
This can be used by maven via the following:
mvn plugin:goal#second-cli
mvn plugin:goal#first-cli
See also the release notes for Maven 3.3.1.

Nar dependency in multi-module maven project

I set up a multi module maven project, which is comprised of a module destined to build nar jni library, and a jar packaged module that is dependent on that library.
I am able to install the nar library to my local maven repository, but I fail to use it in dependent module.
For instance, I run mvn nar:nar-unpack and I get:
[INFO] ------------------------------------------------------------------------
[INFO] Building nar-dependent 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- nar-maven-plugin:3.2.0:nar-unpack (default-cli) # nar-dependent ---
[INFO] Unpacking 0 dependencies to /home/przemek/Documents/stimulant/nar-dependent/target/nar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
It seems that there are no nar dependencies, which is obviously not true.
Moreover, trying to execute the main method of the class that makes use of the jni library fails:
mvn exec:java -Dexec.mainClass=App
[INFO] --- exec-maven-plugin:1.4.0:java (default-cli) # nar-dependent ---
[WARNING]
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:293)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.UnsatisfiedLinkError: no nar-library-1.0-SNAPSHOT in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1865)
at java.lang.Runtime.loadLibrary0(Runtime.java:870)
at java.lang.System.loadLibrary(System.java:1122)
at jnibook.NarSystem.loadLibrary(NarSystem.java:23)
at jnibook.HelloWorld.<clinit>(HelloWorld.java:10)
at App.main(App.java:9)
... 6 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
The structure of the project looks like this:
.
├── nar-dependent
│   ├── pom.xml
│   └── src
│   └── main
│   └── java
│   └── App.java
├── nar-library
│   ├── pom.xml
│   └── src
│   ├── main
│   │   ├── c
│   │   │   └── HelloWorld.c
│   │   ├── include
│   │   ├── java
│   │   │   └── jnibook
│   │   │   └── HelloWorld.java
│   │   └── resources
│   └── test
│   └── java
├── parent
│   └── pom.xml
Here is the parent 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>sidec</groupId>
<artifactId>stimulant</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>../nar-library</module>
<module>../nar-dependent</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
The nar-library module 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>
<parent>
<groupId>sidec</groupId>
<artifactId>stimulant</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
<artifactId>nar-library</artifactId>
<packaging>nar</packaging>
<name>nar-library</name>
<properties>
<skipTests>true</skipTests>
</properties>
<build>
<plugins>
<plugin>
<groupId>com.github.maven-nar</groupId>
<artifactId>nar-maven-plugin</artifactId>
<version>3.2.0</version>
<extensions>true</extensions>
<configuration>
<cpp>
<exceptions>false</exceptions>
</cpp>
<libraries>
<library>
<type>jni</type>
<linkCPP>false</linkCPP>
<narSystemPackage>jnibook</narSystemPackage>
</library>
</libraries>
<javah>
<includes>
<include></include>
</includes>
</javah>
</configuration>
</plugin>
</plugins>
</build>
</project>
The nar-dependent 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>
<parent>
<groupId>sidec</groupId>
<artifactId>stimulant</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>
<artifactId>nar-dependent</artifactId>
<packaging>jar</packaging>
<name>nar-dependent</name>
<build>
<plugins>
<plugin>
<groupId>com.github.maven-nar</groupId>
<artifactId>nar-maven-plugin</artifactId>
<version>3.2.0</version>
<extensions>true</extensions>
<!--<executions>-->
<!--<execution>-->
<!--<id>nar-download</id>-->
<!--<goals>-->
<!--<goal>nar-download</goal>-->
<!--</goals>-->
<!--</execution>-->
<!--</executions>-->
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>sidec</groupId>
<artifactId>nar-library</artifactId>
<version>1.0-SNAPSHOT</version>
<type>nar</type>
</dependency>
</dependencies>
</project>
Finally, as a proof that it is really the HelloWorld project, a library class:
package jnibook;
public class HelloWorld {
public native void print();
static {
NarSystem.loadLibrary();
}
}
and a client app:
import jnibook.HelloWorld;
public class App {
public static void main(String ... args){
(new HelloWorld()).print();
}
}
I referenced https://maven-nar.github.io/examples.html with no success.
I have no idea what is going wrong.
Any ideas? Here is zip with project.
This might be an outdated question, but I'll answer all the same :
When running the java JNI app, it must be told where to find the .so library holding the relevant native C code used by JNI.
For example, if you closed your app in the executable jar app.jar :
java -Djava.library.path=[path to the .so native C library] -jar app.jar
PS - you can see that the JVM can't find the native C library thanks to the exception : java.lang.UnsatisfiedLinkError: no nar-library-1.0-SNAPSHOT in java.library.path
I have tried my own variant of your example with version 3.6.0 of the plugin. With that version, I at least get
Unpacking 1 dependencies to /home/karsten/svn/hellotest/target/nar
and the .so gets unpacked when I run mvn nar:nar-unpack in the dependent module.
But the only way I have found to make mvn nar:nar-integration-test work in the dependent module, is by writing
LD_LIBRARY_PATH=target/nar/hellojni-0.0-amd64-Linux-gpp-jni/lib/amd64-Linux-gpp/jni mvn nar:nar-integration-test
I tried several ways of specifying java.library.path, but with no success.

Resources