I have a maven project in which I try to execute code during build (ideally between compile
and test
phases, but I'm not there yet, for now I execute the code after goals execution).
For this purpose, I use :
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
My code is executed during build. However, it uses com.google.guava
reflection, which does not seem to work as expected in this context.
Here is a MRE code :
package com.example;
import com.google.common.reflect.ClassPath;
import java.io.IOException;
public class LoadedClassesPrinter {
public static void main(String[] args) throws IOException {
ClassPath.from(ClassLoader.getSystemClassLoader())
.getAllClasses()
.stream()
.forEach(o -> System.out.println(o.getName()));
}
}
And here is a MRE 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>maven-exec-reflection</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<mainClass>com.example.LoadedClassesPrinter</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<mainClass>com.example.LoadedClassesPrinter</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
When I execute the main class from my IDE or from the generated jar (with dependencies), it works : it prints a bunch of classes, including my LoadedClassesPrinter
, as I expect.
...
com.google.j2objc.annotations.RetainedLocalRef
com.google.j2objc.annotations.RetainedWith
com.google.j2objc.annotations.Weak
com.google.j2objc.annotations.WeakOuter
com.example.LoadedClassesPrinter
But when I run mvn clean install assembly:single exec:java
, the main method is executed, but it only prints a much smaller list of classes, which, to my surprise, does not include my LoadedClassesPrinter
:
org.codehaus.classworlds.BytesURLConnection
org.codehaus.classworlds.BytesURLStreamHandler
org.codehaus.classworlds.ClassRealm
org.codehaus.classworlds.ClassRealmAdapter
org.codehaus.classworlds.ClassRealmReverseAdapter
org.codehaus.classworlds.ClassWorld
org.codehaus.classworlds.ClassWorldAdapter
org.codehaus.classworlds.ClassWorldReverseAdapter
org.codehaus.classworlds.ConfigurationException
org.codehaus.classworlds.Configurator
org.codehaus.classworlds.ConfiguratorAdapter
org.codehaus.classworlds.DefaultClassRealm
org.codehaus.classworlds.DuplicateRealmException
org.codehaus.classworlds.Launcher
org.codehaus.classworlds.NoSuchRealmException
org.codehaus.plexus.classworlds.ClassWorld
org.codehaus.plexus.classworlds.ClassWorldException
org.codehaus.plexus.classworlds.ClassWorldListener
org.codehaus.plexus.classworlds.UrlUtils
org.codehaus.plexus.classworlds.launcher.ConfigurationException
org.codehaus.plexus.classworlds.launcher.ConfigurationHandler
org.codehaus.plexus.classworlds.launcher.ConfigurationParser$1
org.codehaus.plexus.classworlds.launcher.ConfigurationParser
org.codehaus.plexus.classworlds.launcher.Configurator$1
org.codehaus.plexus.classworlds.launcher.Configurator
org.codehaus.plexus.classworlds.launcher.Launcher
org.codehaus.plexus.classworlds.realm.ClassRealm
org.codehaus.plexus.classworlds.realm.DuplicateRealmException
org.codehaus.plexus.classworlds.realm.Entry
org.codehaus.plexus.classworlds.realm.NoSuchRealmException
org.codehaus.plexus.classworlds.strategy.AbstractStrategy
org.codehaus.plexus.classworlds.strategy.OsgiBundleStrategy
org.codehaus.plexus.classworlds.strategy.ParentFirstStrategy
org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
org.codehaus.plexus.classworlds.strategy.Strategy
org.codehaus.plexus.classworlds.strategy.StrategyFactory
I found a set of configuration for exec-maven-plugin
and I tried to use them but it never changed anything.
What am I doing wrong? Is there an easy way to execute code to list my classes using reflection during build? It does not have to be exec-maven-plugin
and com.google.guava
, I am open to other plugins and libraries.
(I use maven 3.8.3 and java 16)
Aucun commentaire:
Enregistrer un commentaire