Maven classifier empty jars - java

Assume we have multimodule maven project:
parent
|-module-a-jar
|-module-b-jar
|-web-module-c-war
There is common classified specified for parent pom.xml:
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<classifier>${my.project.classifier}</classifier>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<classifier>${my.project.classifier}</classifier>
</configuration>
</plugin>
</plugins>
</pluginManagement>
Assume that I build project using
mvn clean package -Dmy.project.classifier=NIGHTLY
After building web-module-c-war contains empty folders instead of jar files:
web-module-c-war
|-WEB-INF
|-lib
|-module-a-jar
|-module-a-jar
Can you please advise how to fix this? Why this is happening?
If I remove classifier from maven-jar-plugin configuration it seems to be working fine.
Thanks

why not just make this?
pom web-module-c-war :
<groupId>xxxxx</groupId>
<artifactId>xxxx</artifactId>
<version>1.0.0-SNAPSHOT</version>
<classifier>${my.project.classifier}</classifier>
..
..
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>module-a-jar</artifactId>
<version>${project.version}</version>
<classifier>${my.project.classifier}</classifier>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>module-b-jar</artifactId>
<version>${project.version}</version>
<classifier>${my.project.classifier}</classifier>
</dependency>

Related

Include JDBC driver for Postgres in the JAR fo my simple demo app driven by Maven [duplicate]

This question already has answers here:
How can I create an executable/runnable JAR with dependencies using Maven?
(33 answers)
Closed 3 years ago.
How to make Maven include the JDBC driver for Postgres inside my app's .jar file?
I added this dependency element to the <dependencies> element in my POM.
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.8</version>
</dependency>
The IntelliJ IDE shows the driver was successfully downloaded, as it is listed in the "External Libraries" item of my Project pane. And my code can use the JDBC classes such as PGSimpleDataSource.
When I build, if I look inside the resulting .jar file, there is no JDBC driver included.
My project is driven by Maven, using the maven-archetype-quickstart archetype. I did update all the version numbers within the POM to the latest. My only other change was to add the following to get the manifest file of the JAR to specify a main class.
<configuration>
<archive>
<manifest>
<mainClass>work.basil.example.App</mainClass>
</manifest>
</archive>
</configuration>
I thought that Maven by default would bundle all dependencies inside the resulting JAR file. That is the behavior I have seen in building Vaadin web apps. Is that not the case more generally? Or is the JDBC driver special and being omitted for some reason.
If it helps, here is the entire 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>tryjdbc</artifactId>
<version>1.0-SNAPSHOT</version>
<name>tryjdbc</name>
<description>A simple tryjdbc.</description>
<!-- 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>13</maven.compiler.source>
<maven.compiler.target>13</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.6.0-M1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.8</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.8.2</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<archive>
<manifest>
<mainClass>work.basil.example.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.0.0-M1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<reporting>
<plugins>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
</plugin>
</plugins>
</reporting>
</project>
The .war files, such as those you saw in building Vaadin web apps, do include dependencies by default.
In contrast, the .jar files built by Maven do not include any dependencies by default.
You can use a plugin such as maven-shade-plugin to create a shaded jar, which does include the dependencies:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<!-- put your configurations here -->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
Further examples can be found on the Apache Maven Shade Plugin project page.

Maven Deploying Project as Jar - Missing Class definition

I am trying to create a runnable jar from a maven project created in Intellij Idea.
I tried building the Artifact through Intellij, but that did not work out. It could not find the main file.
After that I tried it through maven with:
- mvn compile
- mvn package
This creates a runnable jar which executes, but later when parsing a csv it throws an Exception:
Exception in thread "JavaFX Application Thread"
java.lang.NoClassDefFoundError: org/apache/commons/csv/CSVParser
But it is added in my pom.xml... I downloaded everything even the docs. I can see the org.apache.commons:commons-csv package in the external libraries, but it seems to be missing when creating the jar.
<?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>
<packaging>jar</packaging>
<groupId>sceosa</groupId>
<artifactId>CEO_SA_ReportingLine</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<finalName>CEOSA</finalName>
<plugins>
<!-- download source code in Eclipse, best practice -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<!-- Set a compiler level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- Make this jar executable -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<mainClass>de.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.hsqldb/hsqldb -->
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>25.1-jre</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Can anybody see what is wrong with the pom file or intellij?
By default maven does not include dependencies when building jars. The jar will only work if you have the dependency in some other way.
You can use the maven-assembly-plugin to build a jar-with-dependencies.
I have used the maven-shade-plugin and it works great if you want to bundle all your dependencies into one jar. All you need is the plugin and provide the Main class from your project and it should have a main method. It will look something like this:
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
        <execution>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <shadedArtifactAttached>true</shadedArtifactAttached>
                <transformers>
                    <transformer implementation=
                      "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                        <mainClass>com.test.MainClass</mainClass>
                </transformer>
            </transformers>
        </configuration>
        </execution>
    </executions>
</plugin>
And just build your project using mvn clean install and you should find the the jar in your target directory and it will be suffixed with -shaded.jar. Hope this helps

How to build project like Eclipse Clean on Maven pom.xml

I need help on running maven project. I can run the project on Eclipse, and I tried to deploy / run it on Jenkins CI but without success.
I made a maven project with custom folder structure (not following the default).
Below is the structure:
I can run the project successfully through Eclipse by cleaning the project first where I believe Eclipse build all java files for me. Below is the 'target' folder looks like after I clean the project through Eclipse (not maven clean)
However, when I delete the content of 'target' folder, and run maven -clean and maven -install I received an error like this:
I believe it's because I did not set anything on the pom.xml about building all java classes. However I do not know how to do it ? Can you help me? Changing structure is avoided right now. Thanks
Below is the 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>bukalapak</groupId>
<artifactId>bukalapak</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.46.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>2.46.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<!-- <resources>
<resource>
<directory>src/bukalapak</directory>
</resource>
</resources> -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
<forkMode>never</forkMode>
<suiteXmlFiles>
<suiteXmlFile>testsuite/TestSuiteBukalapak.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
</project>
You can use maven-compiler-plugin. Example:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
If you don't use the default directory structure, you have to configure the source directory appropriately. In your case, the correct configuration would be:
<build>
<sourceDirectory>src</sourceDirectory>
</build>
Otherwise Maven would look in the default folder structure.

Disable main maven artifact install in local repo but not secondary artifact

I have a maven project which generates 2 artifacts.
1 main artifact - a war (packaging element in pom.xml) and
1 secondary artifact - a jar (using maven-jar-plugin) comprised of subset of classes that needs to be shared with other projects.
When I run mvn install, I need to only install the jar artifact and skip the war, and also generate the pom so that the installed jar can be imported by other projects.
I can generate the war and jar artifacts, but both the war and jar are installed in my local maven repo and when I try to import from another project it only shows the war.
I tried a few approaches but I have not been able to solve this.
<project>
<groupId>com.company.component</groupId>
<version>0.0.1-SNAPSHOT</version>
<artifactId>some-artifact</artifactId>
<packaging>war</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<configuration>
<includes>
<include>com/company/component/common/*.class</include>
</includes>
<finalName>component-</finalName>
<classifier>common</classifier>
</configuration>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.13.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>

Maven: add dependecy also with a specific profile

I have 3 projects:
myLibProject: has 2 profiles, "profileOne" and "profileTwo": each profile compile a different jar.
myFirstProject: has the jar compiled by myLibProject with "profileOne" as dependecy
mySecondProject: has the jar compiled by myLibProject with "profileTwo" as dependecy.
It's possible to add myLibProject with a custom profile as depedency?
myLibProject fragment profile: (it's just a sample)
<profiles>
<profile>
<id>profileOne</id>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*prop*</exclude>
</excludes>
<finalName>jarFromProfileOne-${project.version}</finalName>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>compimpl</id>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/*.sql</exclude>
</excludes>
<finalName>jarFromProfileTwo-${project.version}</finalName>
</configuration>
</plugin>
</plugins>
</build>
</profile>
myFirstProject has as depency
<dependency>
<groupId>com.mywebsite</groupId>
<artifactId>myLibProject</artifactId>
<version>1.0.0</version>
<type>pom</type>
<with-profile>profileOne</with-profile>
</dependency>
mySecondProject has as depency
<dependency>
<groupId>com.mywebsite</groupId>
<artifactId>myLibProject</artifactId>
<version>1.0.0</version>
<type>pom</type>
<with-profile>profileTwo</with-profile>
</dependency>
how can I achieve this result? Of course i know "with-profile" does not exists as option.
I need 2 different jars because Documentum's requirment, I know Maven it's created to make a single jar... so I can't use envirnment vars or scope.
Maven 3.0.1

Categories

Resources