SpringBoot: can't create war with maven: Unable to find main class - spring

I have a SpringBoot project with the war and ear module, that I build using maven ( a build automation tool used primarily for Java projects.) and IntelliJ IDEA
This is my pom.xml of the war project
<?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>
<parent>
<groupId>com.bendiciones</groupId>
<artifactId>bendiciones-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
</parent>
<artifactId>bendiciones-war</artifactId>
<name>bendiciones war</name>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.bendicionesbendiciones</groupId>
<artifactId>bendiciones</artifactId>
<version>${project.version}</version>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-tomcat</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<finalName>bendiciones</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.bendiciones.Application</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<scm>
<tag>bendiciones-parent-4.0.0-SNAPSHOT</tag>
</scm>
</project>
But when I build the class I have this error:
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.5.RELEASE:repackage (repackage) on project bendiciones-war: Execution repackage of goal org.springframework.boot:spring-boot-maven-plugin:2.1.5.RELEASE:repackage failed: Unable to find main class -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.5.RELEASE:repackage (repackage) on project bendiciones-war: Execution repackage of goal org.springframework.boot:spring-boot-maven-plugin:2.1.5.RELEASE:repackage failed: Unable to find main class
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
Even the class exist in the project

Add your main(starterApplication) class. The "Main-Class" in the manifest is actually controlled by the "layout" property of the Spring Boot plugin.
<build>
<plugins>
...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.your.starter.Application</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
And you need to check META-INF/MANIFEST.MF.
Main-Class: org.springframework.boot.loader.WarLauncher
Start-Class: com.your.starter.Application
The Executable War File Structure
example.war
|
+-META-INF
| +-MANIFEST.MF
+-org
| +-springframework
| +-boot
| +-loader
| +-<spring boot loader classes>
+-WEB-INF
+-classes
| +-com
| +-mycompany
| +-project
| +-YourClasses.class
+-lib
| +-dependency1.jar
| +-dependency2.jar
+-lib-provided
+-servlet-api.jar
+-dependency3.jar
Or Build using parent and child relationships.
<build>
<pluginManagement>
<plugins>
...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
</build>

You are packaging your application in WAR, that indicates your application is Web Application. Thus, please check or add below important Spring Boot dependency in pom.xml for same:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

Related

SpringBoot: can't create ear with maven: Unable to deduce layout

I have a SpringBoot project with the war and ear module, that I build using maven ( a build automation tool used primarily for Java projects.) and IntelliJ IDEA This is my pom.xml of the war project:
<?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>
<parent>
<groupId>com.bendiciones</groupId>
<artifactId>bendiciones-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
</parent>
<artifactId>bendiciones-ear</artifactId>
<packaging>ear</packaging>
<dependencies>
<dependency>
<groupId>com.bendiciones</groupId>
<artifactId>bendiciones-war</artifactId>
<version>${project.version}</version>
<type>war</type>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.10.1</version>
<configuration>
<finalName>${project.build.finalName}-${project.version}</finalName>
<earSourceDirectory>src/main/resources</earSourceDirectory>
<packagingExcludes>**/*.jar</packagingExcludes>
<modules>
<webModule>
<groupId>com.bendiciones</groupId>
<artifactId>bendiciones-war</artifactId>
<contextRoot>/bendiciones</contextRoot>
<bundleFileName>bendiciones.war</bundleFileName>
</webModule>
</modules>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<releaseProfiles>release</releaseProfiles>
<autoVersionSubmodules>true</autoVersionSubmodules>
<preparationGoals>clean install</preparationGoals>
<arguments>-DaltDeploymentRepository=releases::default::http://el1881.bc:8081/nexus/content/repositories/MicroserviceReleases</arguments>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<layout>NONE</layout>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<finalName>bendiciones</finalName>
</build>
</project>
But when I build the class I have this error:
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.5.RELEASE:repackage (repackage) on project bendiciones-ear: Execution repackage of goal org.springframework.boot:spring-boot-maven-plugin:2.1.5.RELEASE:repackage failed: Unable to deduce layout for '/bendiciones/bendiciones-ear/target/bendiciones-4.0.0-SNAPSHOT.ear' -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.5.RELEASE:repackage (repackage) on project bendiciones-ear: Execution repackage of goal org.springframework.boot:spring-boot-maven-plugin:2.1.5.RELEASE:repackage failed: Unable to deduce layout for '/bendiciones/bendiciones-ear/target/bendiciones-4.0.0-SNAPSHOT.ear'
at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
Try removing the following from your top pom...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

Pitest report-aggregate goal complains about transitive dependencies

I integrated Pitest according to https://pitest.org/aggregating_tests_across_modules/ in a Maven module project (Maven version 3.5.4).
When running mvn clean package the log shows the Pitest execution for the target modules (i.e. the modules for which Pitest should be executed) and I confirmed that result files are actually produced.
However, I get the following error message for the report module:
[ERROR] Failed to execute goal org.pitest:pitest-maven:1.5.1:report-aggregate (put-it-together) on project mutationtesting:
An error has occurred in PIT Test Report report generation.:
.../.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.10.0/jackson-databind-2.10.0.jar is not a directory
jackson-databind is a dependency of the target modules and therefore a transitive dependency of the reporting module. It seems to me that the report-aggregate goal also tries to analyse the transitive dependencies of the modules for which Pitest is enabled.
Is there some error in the project configuration? Is this a known bug? What can I do?
I tried to change the scope of the target modules in the reporting module to provided, and I also set the packing of the reporting module to pom, but to no avail.
Configuration
I configured Pitest in the parent POM as follows:
<properties>
...
<pitest.version>1.5.1</pitest.version>
<pitest.junit5.version>0.12</pitest.junit5.version>
<properties>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<version>${pitest.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>mutationCoverage</goal>
</goals>
</execution>
</executions>
<configuration>
<exportLineCoverage>true</exportLineCoverage>
<outputFormats>
<value>XML</value>
</outputFormats>
<timestampedReports>false</timestampedReports>
</configuration>
<dependencies>
<dependency>
<groupId>org.pitest</groupId>
<artifactId>pitest-junit5-plugin</artifactId>
<version>${pitest.junit5.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</pluginManagement>
The target module POM's each contain this section:
<build>
<plugins>
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
</plugin>
</plugins>
</build>
And finally the reporting module POM looks like this
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
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>
...
</parent>
<artifactId>mutationtesting</artifactId>
<dependencies>
<dependency>
target module 1
</dependency>
<dependency>
target module 2
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.pitest</groupId>
<artifactId>pitest-maven</artifactId>
<executions>
<execution>
<id>put-it-together</id>
<goals>
<goal>report-aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

New GWT maven plugin

I'm getting the error below when when I try to compile.
The goal is gwt:compile
I do set the moduleName as a variable.
The module name is com.example.app.App
Same thing command line ~/work/projects/gwt/app$ mvn gwt:compile "-DmoduleName=com.example.app.App"
Failed to execute goal
net.ltgt.gwt.maven:gwt-maven-plugin:1.0-rc-6:compile (default-cli) on
project mysandbox: The parameters 'moduleName' for goal
net.ltgt.gwt.maven:gwt-maven-plugin:1.0-rc-6:compile are missing or
invalid -> [Help 1]
On the other hand mvn package worked.
Here's my 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>com.example</groupId>
<artifactId>app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<prerequisites>
<maven>${mavenVersion}</maven>
</prerequisites>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<mavenVersion>3.0</mavenVersion>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.gwt</groupId>
<artifactId>gwt</artifactId>
<version>2.8.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>net.ltgt.gwt.maven</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<inherited>false</inherited>
<configuration>
<launcherDir>${project.build.directory}/gwt/launcherDir</launcherDir>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<!-- Do not upgrade past 3.1 to avoid triggering https://issues.apache.org/jira/browse/MSOURCES-95 -->
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.3.14.v20161028</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>net.ltgt.gwt.maven</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<version>1.0-rc-6</version>
<extensions>true</extensions>
<configuration>
<sourceLevel>1.8</sourceLevel>
<failOnError>true</failOnError>
</configuration>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<phase>package</phase>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat6-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<modules>
<module>app-client</module>
<module>app-shared</module>
<module>app-server</module>
</modules>
</project>
So, you have a multi-module Maven project. Invoking mvn gwt:compile will try to execute that "goal" on each of the 4 modules (root module and 3 submodules). Because your moduleName property (used to configure the moduleName property of the gwt:compile goal) likely only exists in the app-client submodule, gwt:compile fails when applied to the root module or the app-shared submodule.
If you want to build your project, run mvn package (and if you don't want to run tests, pass -DskipTests).
Technically, you could also run mvn gwt:compile, but directly inside the submodule. For that to work, you'd first have to mvn install the app-shared submodule; otherwise Maven won't be able to resolve the dependency (as you would no longer be executing the full "reactor build".
For many reasons (see http://blog.lexspoon.org/2012/12/recursive-maven-considered-harmful.html and http://blog.ltgt.net/maven-is-broken-by-design/ as starting points), I highly discourage this practice (mvn install is an anti-pattern; most of the time what you want is actually mvn verify); and I also discourage using any phase before package with multi-module builds (which boils down to only ever using mvn package, possibly with -DskipTests, and mvn verify)

IntelliJ and Maven shader plugin with multiple modules

I'm trying to build a fat jar witht he maven shader plugin. I am going a bit in circles fixing maven and breaking IntelliJ build, and viceversa.
My project is as follows (I left out the plugin section, which contains the shader plugin):
module-a - main project module, contains main class
module-b - module, used by A
module-c - module, used by A
module A pom:
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<groupId>com.my</groupId>
<artifactId>module-a</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>../module-b</module>
<module>../module-c</module>
</modules>
<dependencies>
<dependency>
<groupId>com.my</groupId>
<artifactId>module-b</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.my</groupId>
<artifactId>module-c</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.clapper</groupId>
<artifactId>grizzled-slf4j_2.11</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-http-experimental_2.11</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-http-core-experimental_2.11</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-stream-experimental_2.11</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>net.ceedubs</groupId>
<artifactId>ficus_2.11</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>com.typesafe</groupId>
<artifactId>config</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_2.11</artifactId>
<version>2.4.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<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>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>allinone</shadedClassifierName>
<artifactSet>
<includes>
<include>*:*</include>
</includes>
</artifactSet>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>reference.conf</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.my.Service</Main-Class>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
module B and C poms are similar(except for the module names)
<?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>com.my</groupId>
<artifactId>module-b</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencies>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_2.11</artifactId>
<version>2.4.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<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>
</plugins>
</build>
</project>
All modules are in the same directory at the same level. Although I can build via IntelliJ just fine, I cannot build module-a via maven, because it cannot find modules -b and -c. So in general, I can't build anything that references a local project, since Maven won't find them. Doesn't matter if I try it inside the IDE or from the command line.
Building module-a I get:
[ERROR] Failed to execute goal on project module-a: Could not resolve
dependencies for project com.my:module-a:pom:1.0-SNAPSHOT: The
following artifacts could not be resolved:
com.my:module-b:jar:1.0-SNAPSHOT, com.my:module-c:jar:1.0-SNAPSHOT:
Could not find artifact com.my:module-c:jar:1.0-SNAPSHOT -> [Help 1]
I realize I probably screwed up these poms, trying to play with dependencies vs. module references, but what is the proper way to reference a local module in a pom file, without having to install the module in a repository?
I read that I should be creating a fourth parent project which has module-a as parent, and use that project to create the shaded jar, but even with that approach, I'm failing to get it to locate modules in the same project, it only resolves repo modules.
<packaging>pom</packaging>
<modules>
<module>../module-b</module>
<module>../module-c</module>
</modules>
this part of your module-a's pom reflects that it is the parent of module-b and module-c but contrarily your module-b and c are missing the
<parent>
<groupId>com.my</groupId>
<artifactId>module-a</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
Just an understanding, your module's pom shall include those dependencies which you might want to use as a library for the same. So I hope you want to use some code from module-b/c on your module-a to include them in your pom dependencies.
Or you may want to remove these lines from your module-a
<modules>
<module>../module-b</module>
<module>../module-c</module>
</modules>
considering all the modules are independent of each other in terms of hierarchy.

Simplistic using Groovy in Maven

In some maven project I'll try to figure out how can be used Groovy language. For instance, I try to create a directory in validation phase, by using something like that
<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">
...
<dependencies>
...
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.5.5</version>
<type>pom</type>
<scope>runtime</scope>
</dependency>
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<id>Project</id>
<phase>validate</phase>
<goals>
<goal>execute</goal>
</goals>
</execution>
</executions>
<configuration>
<source>
final FileTreeBuilder treeBuilder = new FileTreeBuilder(new File('tree'))
def folder = new File( 'sample_dir' )
if(!folder.exists()){
treeBuilder.dir('sample_dir')
}
</source>
</configuration>
</plugin>
...
</plugins>
</build>
But if it using the command mvn compile will be present error
[ERROR] script1.groovy: 3: unable to resolve class FileTreeBuilder
What have I missed and what should be added to make this plugin functional?
https://groovy.github.io/gmaven/groovy-maven-plugin/
Customizing Groovy Version
To customize the version of Groovy the plugin will use, override the org.codehaus.groovy:groovy-all dependency on the plugin definition in the project.
For example to use Groovy 2.0.6 instead of the default:
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>groovy-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.0.6</version>
</dependency>
</dependencies>
</plugin>

Resources