Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@
<artifactId>maven-settings</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;

import io.smallrye.classfile.ClassFile;
import io.smallrye.classfile.ClassModel;
Expand All @@ -19,14 +20,28 @@
public abstract class AbstractTransformMojo extends AbstractMojo {
private static final ClassFile cf = ClassFile.of(ClassFile.StackMapsOption.STACK_MAPS_WHEN_REQUIRED);

/**
* Skip the transformation.
*/
@Parameter(defaultValue = "false", property = "smallrye.ffm.skip")
private boolean skip;

/**
* Construct a new instance.
*/
AbstractTransformMojo() {
}

public void execute() throws MojoExecutionException, MojoFailureException {
if (skip || skipped()) {
getLog().info("Skipping the transformation");
return;
}
Path classes = classesDirectory().toPath();
if (!Files.isDirectory(classes)) {
getLog().info("Nothing to transform: " + classes + " does not exist");
return;
}
getLog().info("Transforming classes in path: " + classes);
int cnt = processPath(classes);
switch (cnt) {
Expand Down Expand Up @@ -74,4 +89,11 @@ private int processPath(final Path path) throws MojoFailureException {
}

abstract File classesDirectory();

/**
* {@return {@code true} if the goal has its own reason to skip, beyond the {@code skip} parameter}
*/
boolean skipped() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ public final class TestTransformMojo extends AbstractTransformMojo {
@Parameter(defaultValue = "${project.build.testOutputDirectory}", required = true, property = "smallrye-ffm.testClassesDirectory")
private File classesDirectory;

/**
* Skip the transformation of the test classes when the build skips the tests and their compilation.
*/
@Parameter(defaultValue = "false", property = "maven.test.skip")
private boolean mavenTestSkip;

/**
* Construct a new instance.
*/
Expand All @@ -26,4 +32,9 @@ public TestTransformMojo() {
File classesDirectory() {
return classesDirectory;
}

@Override
boolean skipped() {
return mavenTestSkip;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package io.smallrye.ffm.maven;

import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.lang.reflect.Field;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import org.apache.maven.plugin.logging.Log;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

/**
* The transform goals skip on request and do nothing for a missing classes directory.
*/
public final class TransformMojoSkipTest {

@TempDir
Path dir;

public TransformMojoSkipTest() {
}

@Test
public void testMissingClassesDirectoryIsNotAnError() throws Exception {
CapturingLog log = new CapturingLog();
TransformMojo mojo = mojo(new TransformMojo(), dir.resolve("classes").toFile(), log);
mojo.execute();
assertTrue(log.contains("does not exist"), log.lines.toString());
}

@Test
public void testMissingTestClassesDirectoryIsNotAnError() throws Exception {
CapturingLog log = new CapturingLog();
TestTransformMojo mojo = mojo(new TestTransformMojo(), dir.resolve("test-classes").toFile(), log);
mojo.execute();
assertTrue(log.contains("does not exist"), log.lines.toString());
}

@Test
public void testSkipParameter() throws Exception {
CapturingLog log = new CapturingLog();
TransformMojo mojo = mojo(new TransformMojo(), dir.toFile(), log);
set(mojo, AbstractTransformMojo.class, "skip", true);
mojo.execute();
assertTrue(log.contains("Skipping"), log.lines.toString());
}

@Test
public void testTestGoalHonoursMavenTestSkip() throws Exception {
CapturingLog log = new CapturingLog();
TestTransformMojo mojo = mojo(new TestTransformMojo(), dir.toFile(), log);
set(mojo, TestTransformMojo.class, "mavenTestSkip", true);
mojo.execute();
assertTrue(log.contains("Skipping"), log.lines.toString());
}

@Test
public void testEmptyDirectoryIsTransformed() throws Exception {
CapturingLog log = new CapturingLog();
TransformMojo mojo = mojo(new TransformMojo(), dir.toFile(), log);
mojo.execute();
assertTrue(log.contains("No classes transformed"), log.lines.toString());
}

private static <M extends AbstractTransformMojo> M mojo(M mojo, File classes, Log log) throws Exception {
set(mojo, mojo.getClass(), "classesDirectory", classes);
mojo.setLog(log);
return mojo;
}

private static void set(Object target, Class<?> owner, String name, Object value) throws Exception {
Field field = owner.getDeclaredField(name);
field.setAccessible(true);
field.set(target, value);
}

/**
* A log that keeps every message.
*/
static final class CapturingLog implements Log {
final List<String> lines = new ArrayList<>();

boolean contains(String text) {
return lines.stream().anyMatch(line -> line.contains(text));
}

private void add(CharSequence content) {
lines.add(String.valueOf(content));
}

public boolean isDebugEnabled() {
return true;
}

public void debug(CharSequence content) {
add(content);
}

public void debug(CharSequence content, Throwable error) {
add(content);
}

public void debug(Throwable error) {
add(String.valueOf(error));
}

public boolean isInfoEnabled() {
return true;
}

public void info(CharSequence content) {
add(content);
}

public void info(CharSequence content, Throwable error) {
add(content);
}

public void info(Throwable error) {
add(String.valueOf(error));
}

public boolean isWarnEnabled() {
return true;
}

public void warn(CharSequence content) {
add(content);
}

public void warn(CharSequence content, Throwable error) {
add(content);
}

public void warn(Throwable error) {
add(String.valueOf(error));
}

public boolean isErrorEnabled() {
return true;
}

public void error(CharSequence content) {
add(content);
}

public void error(CharSequence content, Throwable error) {
add(content);
}

public void error(Throwable error) {
add(String.valueOf(error));
}
}
}
Loading