I'm not a very experienced Java user in terms of compilation with maven, eclipse, etc...
I'm trying to build a small proyect with some maven dependencies, and my idea is to include all the dependencies in the ganarated Jar.
This is the content of my 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>amgrd</groupId>
<artifactId>testFlink</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>testFlink</name>
<description>testFlink</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<flink.version>0.10.0</flink.version>
<jdk.version>1.8</jdk.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-java</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-streaming-java</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-clients</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-connector-kafka_2.11</artifactId>
<version>0.10.0</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Main-Class>StreamingWordCount</Main-Class>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
I splitted it on three parts to remark the piece of XML that should be configuring the project to include the jars in the generated file.
Then I show the context menu of the project and click on Run as... -> Maven --> And I type the target "package".
The generated jar does not contain dependencies, only pom.xml, manifest and my source code classes.
Thank you in advance
There should be two jars in your target directory after you build. One that only contains your classes, and one with all the classes. The latter has the suffix jar-with-dependencies.jar.
Related
Is there a way in which we import dependency in the pom for a maven project. Then when we package it and run the jar, the Main() method is provided by the dependency we imported and not in the project we are developing ourselves.
Is there a possibility of doing it? Basically, the dependency would load the project developed by me from the classpath and consume some of the interfaces implemented by me, known to it.
I haven't tried it, but you may be able to simply specify the main class in the manifest using maven-jar-plugin, like so:
<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.maventest</groupId>
<artifactId>aproject</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>aproject</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>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.maventest.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
If that doesn't work you could always provide your own class that imports and calls the main method from the desired class.
Can you try this:
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<dependencies>
<dependency>
<!-- Dependency which includes the main class -->
</dependency>
</dependencies>
<configuration>
<mainClass>absolute.name.MainClass</mainClass>
</configuration>
</plugin>
</plugins>
I'm currently trying to port my project to javafx11. For testing, I'm trying to create a runnable jar file with the hello-world-example from the tutorial page. I included the vm-modules, to prevent the error
Error: JavaFX runtime components are missing, and are required to run this application.
In eclipse, everything works fine, but when I export the project as a runnable jar file, the error returns when I try to run the jar.
When I export the jar-file, I choose the same run configuration as the app and I chose to extract required libraries into generated jar.
EDIT: As it was recommended, I'm currently trying to create a runnable jar with maven. The bhild succeds, but I can't execute it.
The pom.xml looks 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Mpp_App</groupId>
<artifactId>Mpp_App</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Dacemo</name>
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.2</version>
<configuration>
<mainClass>Start</mainClass>
</configuration>
</plugin>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>Start</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.jfoenix</groupId>
<artifactId>jfoenix</artifactId>
<version>9.0.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.15</version>
</dependency>
</dependencies>
</project>
On trying to execute it, I get the error
Could not find or laod main class Start
Caused by: java.lang.NoClassDefFoundError: javafx/application/Application
Is the Start.java defined wrongly? In the eclipse project structure, the Start.java lies in the default package.
i have following war project:
<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>
...
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencies>
...
<dependency>
<groupId>com.sixgroup.rdx.common</groupId>
<artifactId>rdx-common-web</artifactId>
<type>war</type>
</dependency>
<dependency>
<groupId>com.sixgroup.rdx.common</groupId>
<artifactId>rdx-common-web</artifactId>
<version>${rdx-common.version}</version>
<type>jar</type>
<classifier>classes</classifier>
</dependency>
...
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<!-- <version>3.3</version> -->
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<overlays>
<overlay>
<groupId>com.sixgroup.rdx.common</groupId>
<artifactId>rdx-common-web</artifactId>
<exludes>
<exclude>WEB-INF/lib/*.jar</exclude>
<exclude>WEB-INF/classes/**/*.class</exclude>
</exludes>
</overlay>
</overlays>
</configuration>
</plugin>
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
this generates a beautiful war with the common-web .xhtml Files in the root dir and the classes of the common-web as jar in the web-inf/lib folder.
But now when i build the ear, the overlay of this war is ignored, so the excludes don't work and because of this i get an error when deploying (A component named 'FooBar' is already defined in this module)
This is the webModule in my ear-pom:
<webModule>
<groupId>${project.groupId}</groupId>
<artifactId>rdp-pricedata-web</artifactId>
<bundleFileName>rdp-pricedata-web.war</bundleFileName>
<unpack>true</unpack>
</webModule>
is there a way to force the ear to accept the overlay in the war?
I'm working on a maven based web project. In one module that generates .war file i have some dependencies in POM file, some jars are added to WEB-INF/lib folder. i don't have added jar: commons-dbcp-1.3.jar into POM nor in lib folder, but when i build my project using maven, commons-dbcp-1.3.jar added .war file also i can view it to lib folder in target directory.
Can anyone help me to explain how this jar is being added to war or lib folder in target directory.
I have also checked the "build path" and this jar is not added as an external jar.
I'm using Eclipse(Indigo).
Here is the POM file.
<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.company</groupId>
<artifactId>rpt</artifactId>
<version>4.0.2</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts-core</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.2</version>
</dependency>
<dependency>
<groupId>struts</groupId>
<artifactId>struts</artifactId>
<version>1.1</version>
<optional>false</optional>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20131018</version>
</dependency>
<dependency>
<groupId>com.mind</groupId>
<artifactId>mind-common-framework</artifactId>
<version>1.3.1</version>
<optional>false</optional>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<optional>false</optional>
</dependency>
</dependencies>
<organization>
<name>OrgName</name>
</organization>
<build>
<defaultGoal>package</defaultGoal>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>${project.artifactId}-${project.version}-r${prefix.revision}</finalName>
<attach>false</attach>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
<archive>
<manifestEntries>
<Implementation-Build>${project.version}</Implementation-Build>
<Build-Time>${maven.build.timestamp}</Build-Time>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<targetJdk>1.7</targetJdk>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.build.timestamp.format>yyyy/MM/dd HH:mm</maven.build.timestamp.format>
</properties>
</project>
This jar can possibly be one of the dependencies for running your project and Maven automatically downloads and packages the required dependencies(jars) for the project at the time of compilation and packaging.
DBCP commons jar is provided by Apache for maintaining the pool of connection objects . It is basically used in the applications involving database connections where many users can simultaneously try to access the DB(eg: Web Base Applications involving DB operations) to improve the performance of the application and the system.
If you don't wish to bundle this jar with your package and such a capability will be provided by the web container where your application will be deployed(connection pooling), you can set the scope of this dependency to be 'provided'.
The scope you should use for this is "provided". This indicates to Maven that the dependency will be provided at run time by its container or the JDK, for example.
Dependencies with this scope will not be passed on transitively, nor will they be bundled in an package such as a WAR, or included in the runtime classpath.
https://maven.apache.org/general.html#scope-provided
I have found this solution on stackoverflow. Warning - Build path specifies execution environment J2SE-1.4
I am getting almost the same problem but instead of 1-4 i am having 1-6. Unfortunately I don't really understand this configuration thing. It's new thing to me. I am trying second answer to get this working. I found this pom.xml in src under my project and it's xml looks 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>eu.jpereira.trainings.designpatterns.creational.singleton</groupId>
<artifactId>singleton</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>singleton</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>4.9</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- Configure Build Process -->
<build>
<plugins>
<!-- Compiler plugin to use Java 1.6 compatiblity -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<!-- Eclipse plugin to force download of source and JavaDoc jars -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<wtpversion>2.0</wtpversion>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
</plugins>
</build>
</project>
What and where should I add sth? I am using Eclipse on Windows 7. My version of java is: 1.8.0.25-b18 Words like plugin and Maven and JUnit are highlighted red. I would be grateful for help!
Try
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.4</source>
<target>1.4</target>
</configuration>
</plugin>
And then regenerate the Eclipse project with mvn eclipse:eclipse.