I have Maven project on eclipse, and the project has multiple main entry point within different packages. How can i create .jar correctly so I can choose which main method to run?
I tried creating jar using maven install, but when I run it cannot find or load the main class, and I am running .jar like this.
java jar myproject-0.0.1-SNAPSHOT.jar com.mycompany.project.Main
I also tried this andstill same error
java -cp myproject-0.0.1-SNAPSHOT.jar com.mycompany.project.Main
here is 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>edu.cpp.cs499</groupId>
<artifactId>Netflix-MapReduce</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Netflix-MapReduce</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>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-core</artifactId>
<version>1.2.1</version>
</dependency>
</dependencies>
</project>
Related
I did not want to ask this question but I tried all the possible solutions on the StackOverFlow. With all the codes I tried the problem was:
Here is my 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ahmetbesli</groupId>
<artifactId>nobet</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>nobetci</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20190722</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I am trying to create the jar file with Intellij build artifacts.
I tried to add maven plugin for creating jar file.
I tried to define the mainClass also. But it didn't work.
spring-boot Maven: How to create executable jar with main class?
Create Jar of maven spring boot project
Build jar and run spring boot from cmd
Thanks for your help.
You should NOT use task jar in IntelliJ like that:
Instead, we can build jar by this command: ./mvnw clean package
And start service as usual: java -jar demo-0.0.1-SNAPSHOT.jar
Update: Also can use package in IntelliJ:
I was learning about "import" scope in maven and did a sample project below,
Project_1 POM file :
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
project_2 POM file :
<parent>
<groupId>com.sample</groupId>
<artifactId>project1</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
...
...
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.sample</groupId>
<artifactId>project1</artifactId>
<version>1.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
However, this throws an error stating that the JUnit packages are not available in Project2. When I remove the dependencyManagement tag from project_1 POM file. Everything works fine.
But as per the maven docs,
This scope is only supported on a dependency of type pom in the
section. It indicates the dependency to be
replaced with the effective list of dependencies in the specified
POM's section. Since they are replaced,
dependencies with a scope of import do not actually participate in
limiting the transitivity of a dependency.
I've done as mentioned in the docs, what went wrong here?
It looks like you're trying to use a bill-of-materials (BOM) POM and import that.
Your Project_1 is the BOM POM in this case, and you include all your project's dependencies in the <dependencyManagement> element. It looks like you're doing this correctly.
To import a BOM POM in your project however, you need both <dependencyManagement> and <dependencies>.
For example, your Project_2 pom.xml should include:
<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.sample</groupId>
<artifactId>project1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencyManagement>
<dependencies>
<!-- Imports the bill-of-materials POM. -->
<dependency>
<groupId>com.sample</groupId>
<artifactId>bom</artifactId>
<version>1.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Add a dependency from the BOM POM.
Note: no version or scope is required, it's inherited from the BOM POM! -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
Here is the BOM POM definition for the above example:
<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.sample</groupId>
<artifactId>bom</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
There is documentation about this on the Apache Maven website (search for "bom").
Update:
BOM POMs and parent POMs are similar but different. BOM POMs are meant purely for importing dependencies into your project. Parent POMs are meant for multi-module projects, and allow you to define Maven coordinates, plugins, and dependencies that will be inherited by all of modules using the parent POM.
Here is an example of using an inherited dependency from a parent POM. Note that there are several elements included here that don't make sense in a BOM 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sample</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>project1</artifactId>
<packaging>jar</packaging>
<dependencies>
<!-- Inherited from the parent POM. -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
Here is the parent POM definition for the above example:
<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.sample</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>child1</module>
<module>child2</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--Global dependencies...-->
</dependencies>
<build>
<pluginManagement>
<!--Plugins...-->
</pluginManagement>
</build>
</project>
First Make sure you are using Maven version 2.0.9 or later that supports this functionality.
Second your Project1 Pom.Xml is missing the attribute "packaging" Make sure your packaging and type are of the same name so that it inherits all the dependencies.
Also your artifact Id and group ID has to be the same as your project 1's Pom.xml file for it to import the dependency
When in doubt refer to this link
https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Importing_Dependencies
I have a maven project with 4 subprojects, let's say the following structure:
Main
|- core (jar)
|- webapp1 (war)
|- webapp2 (war)
|- webapp3 (war)
The three webapp subprojects depends on core subproproject. So the poms are like:
Main (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>myGroup</groupId>
<artifactId>MyArtifact</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<properties>
</properties>
<repositories>
</repositories>
<modules>
<module>core</module>
<module>webapp1</module>
<module>webapp2</module>
<module>webapp3</module>
</modules>
...
</project>
Core (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>
<artifactId>core</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>MyGroup</groupId>
<artifactId>MyArtifact</artifactId>
<version>1.0</version>
</parent>
...
</project>
And web app poms are 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>
<artifactId>webapp1</artifactId>
<version>1.1.0-A1</version>
<packaging>war</packaging>
<parent>
<groupId>MyGroup</groupId>
<artifactId>MyArtifact</artifactId>
<version>1.0</version>
</parent>
<dependencies>
<dependency>
<groupId>MyGroup</groupId>
<artifactId>core</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
...
</project>
Everything worked fine during months when i execute 'mvn package' on the project's root folder and deploy the wars being generated.
But now i want to deploy an Ear instead of separate War files so i added a subproject called earproject. With the following 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>
<artifactId>earproject</artifactId>
<packaging>ear</packaging>
<parent>
<groupId>MyGroup</groupId>
<artifactId>MyArtifact</artifactId>
<version>1.0</version>
</parent>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.10.1</version>
<configuration>
<finalName>EarName</finalName>
<version>5</version>
<generatedDescriptorLocation>${basedir}/src/main/applicatio/META-INF</generatedDescriptorLocation>
<modules>
<webModule>
<groupId>MyGroup</groupId>
<artifactId>webapp1</artifactId>
<bundleFileName>webapp1.war</bundleFileName>
<contextRoot>/webapp1</contextRoot>
</webModule>
<webModule>
<groupId>MyGroup</groupId>
<artifactId>webapp2</artifactId>
<bundleFileName>webapp2.war</bundleFileName>
<contextRoot>/webapp2</contextRoot>
</webModule>
<webModule>
<groupId>MyGroup</groupId>
<artifactId>webapp3</artifactId>
<bundleFileName>webapp3.war</bundleFileName>
<contextRoot>/webapp3</contextRoot>
</webModule>
</modules>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>MyGroup</groupId>
<artifactId>core</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>MyGroup</groupId>
<artifactId>webapp1</artifactId>
<type>war</type>
<version>1.0</version>
</dependency>
<dependency>
<groupId>MyGroup</groupId>
<artifactId>webapp2</artifactId>
<type>war</type>
<version>1.0</version>
</dependency>
<dependency>
<groupId>MyGroup</groupId>
<artifactId>webapp3</artifactId>
<type>war</type>
<version>1.0</version>
</dependency>
</dependencies>
</project>
Whith this configuration when i run 'mvn clean' and 'mvn package' the ear file is generated but the war files inside the ear file contains an invalid old version of the core.jar.
If i check target directories on webapp projects the War files has a right core.jar version. It seems like EAR packaging uses an old version of the jar that is in my mvn repository, but i don't know why, because on the separate war files maven puts the recently created core.jar.
Why is this happening? Do i need to specify anything more on earproject's pom.xml?
I think the problem you are describing can be solved easily. In appearance Maven ear plugin is getting the core jar file from the repository instead of the new compiled one. I don't know the reason of this plugin's behaviour but I found this issue long time ago.
You should install your jar libraries in the local repository by using the following command
mvn clean install
instead of
mvn package
which only generates the artifacts
i want add org.springframework.spring.web-mvc3.2.0.RELEASE dependency in my project . but when i add this dependency and save that , it show this error :
and 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>fit</groupId>
<artifactId>Example01</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Example01 Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.0.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>Example01</finalName>
</build>
</project>
It seems that you lost the connection to Maven central half way during the transfer. The problem is that your artifacts could become out of sync with Maven cache. If your local Maven repository is relatively small I would suggest to delete entire repository folder under .m2 and try to rebuild again from the command line (mvn clean install)
I'm new to Maven but I'm hooked on what it offers. How do I take a webapp and have Maven package the webapp as a deployable WAR? Additionally, can I set up Maven to automatically version the builds?
Here is an example from the getting started guide:
<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.mycompany.app</groupId>
<artifactId>my-webapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>my-webapp</finalName>
</build>
</project>
Change to the project's directory and type:
mvn clean package
target/my-webapp.war is built