Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven adding mainClass in pom.xml with the right folder path

I want to get a working jar file with my maven project.

The build part is:

<build>     <plugins>         <plugin>             <groupId>org.apache.maven.plugins</groupId>             <artifactId>maven-checkstyle-plugin</artifactId>             <version>2.14</version>             <dependencies>                 <dependency>                     <groupId>com.puppycrawl.tools</groupId>                     <artifactId>checkstyle</artifactId>                     <version>6.4.1</version>                 </dependency>             </dependencies>             <configuration>                 <consoleOutput>true</consoleOutput>                 <configLocation>${basedir}/src/test/resources/checkstyle_swt1.xml</configLocation>             </configuration>         </plugin>          <plugin>             <groupId>org.apache.maven.plugins</groupId>             <artifactId>maven-assembly-plugin</artifactId>             <version>2.4</version>             <configuration>                 <descriptor>src/assembly/src.xml</descriptor>             </configuration>             <executions>                 <execution>                     <phase>package</phase>                     <goals>                         <goal>single</goal>                     </goals>                 </execution>             </executions>         </plugin>      </plugins>     <pluginManagement>         <plugins>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-compiler-plugin</artifactId>                 <version>3.0</version>                 <configuration>                     <source>1.7</source>                     <target>1.7</target>                     <archive>                         <manifest>                             <addDefaultImplementationEntries>true</addDefaultImplementationEntries>                             <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>                             <addClasspath>true</addClasspath>                             <mainClass>org.jis.Main</mainClass>                                                         </manifest>                     </archive>                 </configuration>             </plugin>         </plugins>     </pluginManagement> </build> 

So my problem right now is that I don'T know how to implement the mainclass properly into my pom.xml and later into my jar file. The Folder Structure is: src/main/java/org/jis/Main.java but if I add the following line

<mainClass>src.main.java.org.jis.Main</mainClass> 

it doesn't work.

Thanks in advance

like image 653
NhatNienne Avatar asked Apr 28 '15 13:04

NhatNienne


2 Answers

First, your main class doesn't include src/main/java. Look at the package declaration in that Java file. For example, package org.jis;, then add the main class to that. In other words, it's only org.jis.Main.

You need to configure the maven-jar-plugin instead the of the maven-compiler-plugin. The jar-plugin is the one which is responsible for packaging and creating the manifest.MF.

From http://maven.apache.org/shared/maven-archiver/examples/classpath.html

  <build>     <plugins>       <plugin>         <groupId>org.apache.maven.plugins</groupId>         <artifactId>maven-jar-plugin</artifactId>         ...         <configuration>           <archive>             <manifest>               <addClasspath>true</addClasspath>               <mainClass>fully.qualified.MainClass</mainClass>             </manifest>           </archive>         </configuration>         ...       </plugin>     </plugins>   </build> 
like image 138
Martin Baumgartner Avatar answered Sep 26 '22 01:09

Martin Baumgartner


You may mention as below if it's a spring boot application.

<build>     <plugins>         <plugin>             <groupId>org.springframework.boot</groupId>             <artifactId>spring-boot-maven-plugin</artifactId>             <configuration>                 <mainClass>com.test.MainClass</mainClass>             </configuration>                            </plugin>     </plugins> </build> 
like image 28
Vinto Avatar answered Sep 23 '22 01:09

Vinto