Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/test with gradle #21

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ for details.
## Plugin goals

- `prepare-agent` - adds the AppMap Java agent to the JVM
- `validate-config` - Validates the config file is present and redable

## Plugin configuration

Expand Down Expand Up @@ -114,6 +115,12 @@ Example:

## Troubleshooting

**After I add the appmap plugin config in the POM my project won't build**
It's likely the configuration file is not present or is not redable, try
using the validate-config file goal of the maven plugin to check whether
you have a configuration problem or not, run:
`mvn com.appland:appmap-maven-plugin:validate-config`

**I have no `target/appmap` directory**
It's likely that the agent is not running. Double check the `prepare-agent`
goal is being run. If the JVM is being forked at any point, make sure the
Expand Down
18 changes: 9 additions & 9 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ buildscript {
ext {
mavenVersion = "$mavenVersionCore"
}

}

plugins {
Expand Down Expand Up @@ -50,23 +49,23 @@ configurations {
}

dependencies {
implementation 'com.appland:appmap-agent:1.0.3'

implementation 'org.apache.maven:maven-plugin-api:3.6.1'
compileOnly 'org.apache.maven.plugin-tools:maven-plugin-annotations:3.6.0'
compile 'org.apache.maven.plugin-tools:maven-plugin-annotations:3.6.0'
implementation 'org.apache.maven:maven-core:3.5.4'
implementation 'org.apache.commons:commons-lang3:3.10'

testImplementation 'junit:junit:4.13.1'
testImplementation "org.apache.maven.plugin-testing:maven-plugin-testing-harness:3.3.0"
compile 'org.apache.maven:maven-compat:3.6.3'
compile 'commons-io:commons-io:2.6'

compile "org.eclipse.sisu:org.eclipse.sisu.plexus:0.3.3"

// as per https://stackoverflow.com/a/64726155
mavenEmbedder 'org.apache.maven:maven-embedder:3.6.3'
mavenEmbedder 'org.apache.maven:maven-compat:3.6.3'
mavenEmbedder 'org.apache.maven.resolver:maven-resolver-connector-basic:1.4.1'
mavenEmbedder 'org.apache.maven.resolver:maven-resolver-transport-http:1.4.1'
mavenEmbedder 'org.slf4j:slf4j-simple:1.7.30'
// mavenEmbedder 'org.apache.maven.wagon:wagon-http:3.4.1'
// mavenEmbedder 'org.apache.maven.wagon:wagon-provider-api:3.4.1'

implementation 'com.appland:appmap-agent:1.0.4'
}

//group = paramGroupId
Expand Down Expand Up @@ -129,6 +128,7 @@ task generatePluginDescriptor(type: JavaExec, dependsOn: compileJava) {
appendNode('directory', directory)
appendNode('outputDirectory', outputDirectory)
}
asNode().appendNode('version', paramVersion)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dustinbyrne what's fixed by doing this?

}
.writeTo(pomFile)

Expand Down
21 changes: 19 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.appland</groupId>
<artifactId>appmap-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>0.5.0-SNAPSHOT</version>
<version>1.1.2</version>
<name>Appland Java Recorder Maven Plugin</name>
<description>This maven plugin helps you automatically generate an Appland AppMap using the Java Recorder.
</description>
Expand All @@ -21,7 +21,7 @@
<dependency>
<groupId>com.appland</groupId>
<artifactId>appmap-agent</artifactId>
<version>0.5.0</version>
<version>1.0.3</version>
</dependency>

<dependency>
Expand All @@ -46,6 +46,23 @@
<version>4.13.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
<version>3.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<version>3.6.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
</dependencies>

<build>
Expand Down
21 changes: 16 additions & 5 deletions src/main/java/com/appland/appmap/AppMapAgentMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.apache.maven.project.MavenProject;

import java.io.File;
import java.nio.file.Files;
import java.util.*;

import static java.lang.String.format;
Expand All @@ -17,24 +18,28 @@ public abstract class AppMapAgentMojo extends AbstractMojo {
static final String APPMAP_AGENT_ARTIFACT_NAME = "com.appland:appmap-agent";
static final String SUREFIRE_ARG_LINE = "argLine";
static final List<String> DEBUG_FLAGS = Arrays.asList("hooks", "locals", "http");
public static final String DEFAULT_CONFIG_FILE = "appmap.yml";
public static final String DEFAULT_OUTPUT_DIRECTORY = "target/appmap";
public static final String DEFAULT_DEBUG_FILE = "target/appmap/agent.log";
public static final int DEFAULT_EVENT_VALUE_SIZE = 1024;

@Parameter(property = "skip")
protected boolean skip = false;

@Parameter(property = "project.outputDirectory")
protected File outputDirectory = new File("target/appmap");
protected File outputDirectory = new File(DEFAULT_OUTPUT_DIRECTORY);

@Parameter(property = "project.configFile")
protected File configFile = new File("appmap.yml");
protected File configFile = new File(DEFAULT_CONFIG_FILE);

@Parameter(property = "project.debug")
protected String debug = "info";

@Parameter(property = "project.debugFile")
protected File debugFile = new File("target/appmap/agent.log");
protected File debugFile = new File(DEFAULT_DEBUG_FILE);

@Parameter(property = "project.eventValueSize")
protected Integer eventValueSize = 1024;
protected Integer eventValueSize = DEFAULT_EVENT_VALUE_SIZE;

@Parameter(property = "plugin.artifactMap")
protected Map<String, Artifact> pluginArtifactMap;
Expand All @@ -44,7 +49,9 @@ public abstract class AppMapAgentMojo extends AbstractMojo {

public abstract void execute() throws MojoExecutionException;

protected void skipMojo() {
protected void skipMojo(String reason) {
skip = true;
getLog().info(reason);
}

protected void loadAppMapJavaAgent() {
Expand Down Expand Up @@ -131,4 +138,8 @@ protected File getAppMapAgentJar() {
public MavenProject getProject() {
return project;
}

protected boolean isConfigFileValid() {
return configFile.exists() && Files.isReadable(configFile.toPath());
}
}
24 changes: 16 additions & 8 deletions src/main/java/com/appland/appmap/LoadJavaAppMapAgentMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,36 @@
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;

import java.io.FileNotFoundException;
import java.nio.file.Files;

/**
* Goal that adds appmap.jar to JVM execution as javaagent,
* right before the test execution begins.
* Goal that adds appmap.jar to JVM execution as javaagent, right before the test execution begins.
*/
@Mojo(name = "prepare-agent", defaultPhase = LifecyclePhase.TEST_COMPILE)
public class LoadJavaAppMapAgentMojo extends AppMapAgentMojo {

@Override
public void execute()
throws MojoExecutionException {
public void execute() throws MojoExecutionException {
try {
if (skip) {
getLog().info("Skipping AppLand AppMap execution because property skip is set.");
skipMojo();
skipMojo("Skipping AppLand AppMap execution because property skip is set.");
return;
} else if (!isConfigFileValid()) {
skipMojo(
"Skipping AppLand AppMap execution because the config file: "
+ configFile.getPath()
+ " does not exist or is not readable"
);
throw new FileNotFoundException(configFile.getPath());
} else {
getLog().info("Initializing AppLand AppMap Java Recorder." );
getLog().info("Initializing AppLand AppMap Java Recorder.");
loadAppMapJavaAgent();
}
} catch (Exception e) {
getLog().error("Error initializing AppLand AppMap Java Recorder");
e.printStackTrace();
throw new MojoExecutionException("Error initializing AppLand AppMap Java Recorder", e);
}
}

}
30 changes: 30 additions & 0 deletions src/main/java/com/appland/appmap/ValidateConfigMojo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.appland.appmap;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;

import java.io.FileNotFoundException;

@Mojo(name = "validate-config", defaultPhase = LifecyclePhase.TEST_COMPILE)
public class ValidateConfigMojo extends AppMapAgentMojo {

@Override
public void execute() throws MojoExecutionException {
try {
if (!isConfigFileValid()) {
skipMojo(
"Skipping AppLand AppMap execution because the config file: "
+ configFile.getPath()
+ " does not exist or is not readable"
);
throw new FileNotFoundException(configFile.getPath());
} else {
getLog().info("Appland AppMap Configuration file found.");
}
} catch (Exception e) {
getLog().error("Error executing AppLand AppMap Java Recorder");
throw new MojoExecutionException("Error initializing AppLand AppMap Java Recorder", e);
}
}
}
23 changes: 23 additions & 0 deletions src/test/java/com/appland/appmap/GoalLoadTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.appland.appmap;

import org.apache.maven.plugin.testing.AbstractMojoTestCase;
import org.junit.Test;

import java.io.File;

public class GoalLoadTest extends AbstractMojoTestCase {

@Test
public void testPrepareAgentGoalIsLoadedCorrectly() throws Exception {
File pluginPom = new File("build/resources/test/test-project2/pom.xml");
LoadJavaAppMapAgentMojo mojo = (LoadJavaAppMapAgentMojo) lookupMojo("prepare-agent", pluginPom);
assertNotNull(mojo);
}

@Test
public void testValidateConfigGoalIsLoadedCorrectly() throws Exception {
File pluginPom = new File("build/resources/test/test-project2/pom.xml");
ValidateConfigMojo mojo = (ValidateConfigMojo) lookupMojo("validate-config", pluginPom);
assertNotNull(mojo);
}
}
82 changes: 82 additions & 0 deletions src/test/java/com/appland/appmap/PluginLoadTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.appland.appmap;

import org.apache.maven.plugin.Mojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.testing.MojoRule;
import org.junit.Rule;
import org.junit.Test;

import java.io.File;
import java.io.FileNotFoundException;

import static org.junit.Assert.*;

public class PluginLoadTest {

@Rule
public MojoRule rule = new MojoRule() {
@Override
protected void before() throws Throwable {
}

@Override
protected void after() {
}
};

@Test
public void testMojoCanBeInstantiated()
throws Exception {

Mojo mojo = rule.configureMojo(
new LoadJavaAppMapAgentMojo(),
"appmap-maven-plugin",
new File("build/resources/test/test-project/pom.xml")
);
assertNotNull(mojo);
}

@Test
public void testMojoIsLoaded() throws Exception {
LoadJavaAppMapAgentMojo mojo = (LoadJavaAppMapAgentMojo)
rule.configureMojo(
new LoadJavaAppMapAgentMojo(),
"appmap-maven-plugin",
new File("build/resources/test/test-project/pom.xml")
);
assertNotNull(mojo);
}

@Test
public void testMojoFailsWithMissingDefaultConfigFile() throws Exception {
LoadJavaAppMapAgentMojo mojo = (LoadJavaAppMapAgentMojo)
rule.configureMojo(
new LoadJavaAppMapAgentMojo(),
"appmap-maven-plugin",
new File("build/resources/test/test-project/pom.xml")
);
assertNotNull(mojo);
Exception exception = assertThrows(
MojoExecutionException.class,
() -> rule.executeMojo(new File("build/resources/test/test-project/"), "prepare-agent")
);
assertTrue(exception.getCause() instanceof FileNotFoundException);
}

@Test
public void testMojoFailsWithNotExistingConfigFile() throws Exception {
LoadJavaAppMapAgentMojo mojo = (LoadJavaAppMapAgentMojo)
rule.configureMojo(
new LoadJavaAppMapAgentMojo(),
"appmap-maven-plugin",
new File("build/resources/test/test-project/nonexistent_config_file_pom.xml")
);
assertNotNull(mojo);
Exception exception = assertThrows(
MojoExecutionException.class,
() -> rule.executeMojo(new File("build/resources/test/test-project/"), "prepare-agent")
);
assertTrue(exception.getCause() instanceof FileNotFoundException);
}

}
30 changes: 30 additions & 0 deletions src/test/resources/test-project/nonexistent_config_file_pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.github.gino0631</groupId>
<artifactId>appmap-maven-plugin-test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Appmap Plugin Test</name>

<build>
<plugins>
<plugin>
<groupId>com.appland</groupId>
<artifactId>appmap-maven-plugin</artifactId>
<configuration>
<configFile>appmap.yml</configFile>
</configuration>
<executions>
<execution>
<phase>process-test-classes</phase>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Loading