create spring boot native image in a multi module project - spring-boot

i am trying to build an native image with graalvm and spring boot.
my project has several modules.when i try to build native image i got this error:
Error: Please specify class (or <module>/<mainclass>) containing the main entry point method. (see --help)
and when i define mainClass path(org.example.api.Application) in properties in parent pom file i got this error:
Error: Main entry point class 'org.example.api.Application' neither found on the classpath nor on the modulepath.
how can i define the module that contain main class for graalvm?

In your parent pom (the one where you declare all your modules) using the syntax
<modules>
<module>module1</module>
<module>module2</module>
<module>module3</module>
</modules>
use the latest Spring Boot BOM as parent
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
then override the native profile
<profiles>
<profile>
<id>native</id>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
<executions>
<execution>
<id>build-image</id>
<goals>
<goal>compile-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
</profiles>
At this point in your modules (where you need native builds) you can set this build configuration:
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
At this point you will be able to compile the project(s) using the mvn -Pnative clean package

Related

How to Run Command-line main class separately without including existing main-class present in Application in maven based Springboot App?

I am facing bizarre situation. when i run existing main application it also include another commandLine main-class while running.
I have create 2 profile in pom.xml- and also setup mainClass tag.
1- unit-test
2- integration-test
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>${commandLineMainClass}</mainClass>
</configuration>
</plugin>
in integration-test, i have created separate main-class with command-line runner to test the application deployed on another server by just hitting.
i have two issue-
1- when i run existing main-class application, it boot with command-line mainclass also. which should be excluded until this is not run manually by command-line.
when i run command line main class, i m not able to access custom application-{env}.properties, although it is not including existing application main-class which is good.
I am using springBoot maven-plugin also.
please help.
<project>
<parent>
<groupid>org.springframework.boot</groupid>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
<start-class>thisisCommandLineProp</start-class>
<environment>thisisCommandLineProp</environment>
</properties>
<profiles>
<profile>
<id>unittest</id>
<build>
<filename>${project.artifactId}</filename>
</build>
</profile>
<profile>
<id>integrationtest</id>
<build>
<filename>${project.artifactId}</filename>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupid>org.springframework.boot</groupid>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<configuration>
<workingDirectory>${project.basedir}\src\integration-test\</workingDirectory>
<mainClass>${project.basedir}\src\integration-test\${start-class}</mainClass>
<arguments>
<argument>
spring.config.location=${project.basedir}\src\main\resources\application-${environment}.properties</argument>
</arguments>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<project>
**Command - ** spring-boot:run -Denvironment=dev -Dstart-class=com.abc.xyz.Integrationtest -Pintegrationtest

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>

Execution Repackaging: Unable to repackage goal : main class not found

I am doing repackaging in spring I have encountered an error :
Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.1.7.RELEASE:repackage (repackage) on project pet-clinic-data: Execution repackage of goal org.springframework.boot:spring-boot-maven-plugin:2.1.7.RELEASE:repackage failed: Unable to find main class.
Even though I have used true
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<conifguration>
<skip>true</skip>
</conifguration>
</execution>
</executions>
</plugin>
</plugins>
</build>
and the pom is set. As I don't have any main class, how to resolve this error
ALready tried .m2/repository deleted all files and clean code again
When using a multi-module structure, remember that the spring-boot-maven-plugin must be placed in the pom.xml of the module that contains the main class, so that Spring is able to create your jar that, when started, will check that particular class.
If you started the project from Spring Initializr, move this part of code from your main pom.xml to the pom that contains the main class:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
Also, note that you misspelled "configuration" tag (you wrote "conifguration")
I solved this problem for me by adding "pluginManagement" tag after "build" tag in both pom.
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
* --- your code or other configurations --- *
</plugin>
</plugins>
</pluginManagement>
</build>
Alternatively, you can also try adding this property in your data module pom
<properties>
<spring-boot.repackage.skip>true</spring-boot.repackage.skip>
</properties>
Under the <build> section, add the <sourceDirectory> and <testSourceDirectory> to specify the module's source code directories to fix this issue.
<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

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)

Maven - 'override' not work in external settings

I am using spring boot in my app like this:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
In my external settings.xml I defined resource plugin like this:
<profile>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<id>dev</id>
...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<configuration combine.self="override">
<delimiters>
<delimiter>^^</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
</build>
and this not work. When I move it to my pom it works. I know that settings file is red because dev properties are taken from it. What is wrong?

Resources