Blockhound Gradle: 'You need to add '-XX:+AllowRedefinitionToAddDeleteMethods' JVM flag.' - java

I am trying to run Blockhound in my Spring Boot app during testing. Unfortunately gradle does not pick up the required -XX:+AllowRedefinitionToAddDeleteMethods flag. I tried it using IntelliJ's VMoptions in Run Configurations as well as in gradle.properties org.gradle.jvmargs=-XX:+AllowRedefinitionToAddDeleteMethods. The error persists though.

Will this work?
Gradle:
tasks.withType(Test).all {
if (JavaVersion.current().isCompatibleWith​(JavaVersion.VERSION_15)) {
jvmArgs += [
"-XX:+AllowRedefinitionToAddDeleteMethods"
]
}
}
maven:
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<argLine>-XX:+AllowRedefinitionToAddDeleteMethods</argLine>
</configuration>
</plugin>
...
</plugins>
or JAVA_OPTS?
on mac/linux:
export JAVA_OPTS="-XX:+AllowRedefinitionToAddDeleteMethods"
or Windows
set JAVA_OPTS="-XX:+AllowRedefinitionToAddDeleteMethods"
reference: https://github.com/reactor/BlockHound/issues/33

Related

Error when running docker container "NoClassDefFoundError"

I am trying to dockerize a simple Spring Boot Application, built with Maven.
Dockerfile:
FROM openjdk:latest
COPY target/backend-1.0-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","app.jar"]
When I run the .jar without the container (java -jar target/backend-1.0-SNAPSHOT.jar), everything works fine and the app is running.
Now I create the container with docker build -t company/backend .
But when I try to run the docker container with docker run -p 8080:8080 company/backend the following error occurs:
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
at de.company.backend.Application.main(Application.java:10)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 1 more
It seems like docker does not find the main class, even though it is defined in my pom.xml:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<mainClass>de.elbdev.backend.Application</mainClass>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>${mainClass}</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Main Class:
package de.company.backend;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
In your pom.xml, the copy-dependencies goal is specified at the install phase : too late the package of the jar was already done.
I am trying to dockerize a simple Spring Boot Application, built with
Maven.
You don't need to declare any plugin to create a fat jar with spring boot that could be run by a docker container.
Declaring these plugins is error prone (and should be used only in corner cases) while the repackage goal of the spring boot maven plugin attached by default to the package phase of maven will create for you the fat jar :
Repackages existing JAR and WAR archives so that they can be executed
from the command line using java -jar
Juste remove these plugins declarations and execute mvn clean package and it should be good.
Side note :
FROM openjdk:latest
Don't use latest as image version but favor a specific version of the image othewhise you could have bad surprises. As you use JDK 8, you could specify a JRE or a JDK 8 such as : FROM openjdk:8-jre-alpine.
I had the same problem as you.
you need to add plugin in your pom.xml.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
If you input as above, it works normally.
and check MANIFEST.MF (in .jar file)
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: {your main class}

Choosing test category on command line

I am using JUnit's categories to split my tests into different categories and using maven to compile and run my test (surefire and failsafe).
Question is, how to I choose which category of tests are executed from command line?
something like: mvn clean install -DloadTests.
my failsafe plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${failsafe.plugin.version}</version>
<configuration>
<!--Exclude load tests by default-->
<excludedGroups>com.test.lib.categories.LoadTestCategory</excludedGroups>
</configuration>
</plugin>
You could do that with profiles and specify the plugin configuration in there.
<profiles>
<profile>
<id>noLoadTests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${failsafe.plugin.version}</version>
<configuration>
<!--Exclude load tests by default-->
<excludedGroups>com.test.lib.categories.LoadTestCategory</excludedGroups>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
And then run maven
mvn test -PnoLoadTests
If you only ever need to exclude/include one specific category you could also define a property in the profile and use that in the . For more info you can look here
Edit: The other provided answer is the better one in this case, but profiles allow for various advanced configurations.
According to the documentation, user property is called groups. Therefore this should work:
mvn clean install -Dgroups=com.test.lib.categories.LoadTestCategory

maven-javadoc-plugin Dependency

I'm trying to generate javadoc from maven-javadoc-plugin with simple configuration (see below)
Unfortunatly its seems to include all dependencies into javadoc classpath option:
-classpath
'D:/DEV/MyProject/target/classes;C:/Users/me/.m2/repository/com/googlecode/json-simple/json-simple/1.1.1/json-simple-1.1.1.jar; .....'
Then crash with "error in opening zip file"
even when i try to force includeDependencySources to false it does take them all :(
Any idea or good configuration to generate only package like org.mysite.* ?
Thanks
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
<configuration>
<sourcepath>${basedir}/src/main/java</sourcepath>
<includeDependencySources>false</includeDependencySources>
</configuration>
</plugin>

mvn checkstyle:checkstyle uses wrong configuration when using reporting

Im facing the following problem. I have set up my checkstyle with the following configuration:
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${checkstyle.plugin.version}</version>
<inherited/>
<configuration>
<configLocation>${basedir}/checkstyle.xml</configLocation>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
</configuration>
</plugin>
</plugins>
</reporting>
This runs fine when I run mvn site. However, when I run checkstyle through mvn checkstyle:checkstyle in order to get the XML report much more efficiently, the checkstyle plugin fails back to use the default configuration. When I move the plugin to <build> the XML is generated properly, but now the checkstyle report is not included in the generated site anymore.
What is the (current) way of setting up report plugins as Checkstyle, while perserving the ability to run the plugin separately under the same configuration?
Is it really the preferred way to defined your plugins and configuration twice?
Okay, apparently you should add the plugin with configuration to both <build> and <reporting>.

Using Java 7 with official Google Appengine Maven plugin

I'm having trouble using the official Maven Plugin and Java 7 with Google Appengine.
Configuration
My project configuration pom.xml is quite simple:
In the properties section I configure:
<gae.version>1.7.4</gae.version>
And later on I use the plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>${gae.version}</version>
</plugin>
Error message
Whenever I run mvn appengine:update I get the following error:
Unable to update app: The application contains Java 7 classes, but the --use_java7 flag has not been set.
My attempt to solve it
Of course, I tried to fix this issue. Running
mvn appengine:update --use_java7
or
mvn appengine:update -D--use_java7
didn't help, because the flag is not used for the Maven plugin, but instead for the appcfg script.
How do I pass the flag to the script, so that I can use Java 7 (or is there anything else I can do)?
App Engine Java 7 Support is currently for Trusted Tester and not available to public yet, you can apply Trusted Tester at here.
Fortunately the latest official maven plugin does implement this feature, see the AbstractAppCfgMojo.java:
... ...
/**
* Use the App Engine Java 7 runtime for this app.
*
* #parameter
*/
protected boolean useJava7;
... ...
if (useJava7) {
arguments.add("--use_java7");
}
... ...
You can use the following plugin configuration in pom.xml to enable Java7 support:
</build>
<plugins>
... ...
<plugin>
<groupId>com.google.appengine</groupId>
<artifactId>appengine-maven-plugin</artifactId>
<version>${gae.version}</version>
<configuration>
<useJava7>true</useJava7>
</configuration>
</plugin>
</plugins>
</build>

Categories