Skip to content

Commit

Permalink
Fix #2113 create-extension Maven mojo
Browse files Browse the repository at this point in the history
  • Loading branch information
ppalaga committed Jul 11, 2019
1 parent f0dc107 commit 48c0c82
Show file tree
Hide file tree
Showing 21 changed files with 1,267 additions and 0 deletions.
6 changes: 6 additions & 0 deletions devtools/maven/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@
<version>2.14.6</version>
</dependency>

<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>

<!-- extensions reader -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
Expand Down
608 changes: 608 additions & 0 deletions devtools/maven/src/main/java/io/quarkus/maven/CreateExtensionMojo.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package [=javaPackageBase].deployment;

import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.FeatureBuildItem;

class [=artifactIdBaseCamelCase]Processor {

private static final String FEATURE = "[=artifactIdBase]";

@BuildStep
FeatureBuildItem feature() {
return new FeatureBuildItem(FEATURE);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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>
<parent>
<groupId>[=groupId]</groupId>
<artifactId>[=artifactId]-parent</artifactId>
<version>[=version]</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>[=artifactId]-deployment</artifactId>
[#if nameBase?? ] <name>[=namePrefix][=nameBase][=nameSegmentDelimiter]Deployment</name>
[/#if]

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-core-deployment</artifactId>
[#if !assumeManaged ] <version>[=quarkusVersion]</version>
[/#if]
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-processor</artifactId>
<version>[=quarkusVersion]</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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>
[#if grandParentGroupId?? ] <parent>
<groupId>[=grandParentGroupId]</groupId>
<artifactId>[=grandParentArtifactId]</artifactId>
<version>[=grandParentVersion]</version>
<relativePath>[=grandParentRelativePath]</relativePath>
</parent>
[/#if]

[#if groupId?? && groupId != grandParentGroupId ] <groupId>[=groupId]</groupId>
[/#if]
<artifactId>[=artifactId]-parent</artifactId>
[#if groupId?? && groupId != grandParentGroupId && version?? && version != grandParentVersion ] <version>[=version]</version>
[/#if]
[#if nameBase?? ] <name>[=namePrefix][=nameBase][=nameSegmentDelimiter]Parent</name>
[/#if]

<packaging>pom</packaging>
<modules>
<module>deployment</module>
<module>runtime</module>
</modules>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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>
<parent>
<groupId>[=groupId]</groupId>
<artifactId>[=artifactId]-parent</artifactId>
<version>[=version]</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>[=artifactId]</artifactId>
[#if nameBase?? ] <name>[=namePrefix][=nameBase][=nameSegmentDelimiter]Runtime</name>
[/#if]

<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-bootstrap-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-processor</artifactId>
<version>[=quarkusVersion]</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
package io.quarkus.maven;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.junit.Assert;
import org.junit.jupiter.api.Test;

public class CreateExtensionMojoTest {

static CreateExtensionMojo createMojo(String testProjectName) throws IllegalArgumentException,
IllegalAccessException, IOException, NoSuchFieldException, SecurityException {
final Path srcDir = Paths.get("target/test-classes/projects/" + testProjectName);
/*
* We want to run on the same project multiple times with different args so let's create a copy with a random
* suffix
*/
final Path copyDir = Paths
.get("target/test-classes/projects/" + testProjectName + "-" + ((int) (Math.random() * 1000)));
Files.walk(srcDir).forEach(source -> {
try {
Files.copy(source, copyDir.resolve(srcDir.relativize(source)));
} catch (IOException e) {
throw new RuntimeException(e);
}
});

final CreateExtensionMojo mojo = new CreateExtensionMojo();
mojo.basedir = copyDir;
mojo.encoding = CreateExtensionMojo.DEFAULT_ENCODING;
mojo.templatesUriBase = CreateExtensionMojo.DEFAULT_TEMPLATES_URI_BASE;
mojo.quarkusVersion = CreateExtensionMojo.DEFAULT_QUARKUS_VERSION;
mojo.assumeManaged = true;
mojo.nameSegmentDelimiter = CreateExtensionMojo.DEFAULT_NAME_SEGMENT_DELIMITER;
return mojo;
}

@Test
void createExtensionUnderExistingPomMinimal() throws MojoExecutionException, MojoFailureException,
IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, IOException {
final CreateExtensionMojo mojo = createMojo("create-extension-under-existing-pom");
mojo.artifactId = "my-project-(minimal-extension)";
mojo.assumeManaged = false;
mojo.execute();

assertTreesMatch(Paths.get("target/test-classes/expected/create-extension-under-existing-pom-minimal"),
mojo.basedir);
}

@Test
void createExtensionUnderExistingPomCustomGrandParent() throws MojoExecutionException, MojoFailureException,
IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, IOException {
final CreateExtensionMojo mojo = createMojo("create-extension-under-existing-pom");
mojo.artifactId = "my-project-(custom-grand-parent-extension)";
mojo.grandParentArtifactId = "build-bom";
mojo.grandParentRelativePath = "../../build-bom/pom.xml";
mojo.templatesUriBase = "file:templates";
mojo.execute();

assertTreesMatch(
Paths.get("target/test-classes/expected/create-extension-under-existing-pom-custom-grand-parent"),
mojo.basedir);
}

static void assertTreesMatch(Path expected, Path actual) throws IOException {
final Set<Path> expectedFiles = new LinkedHashSet<>();
Files.walk(expected).filter(Files::isRegularFile).forEach(p -> {
final Path relative = expected.relativize(p);
expectedFiles.add(relative);
final Path actualPath = actual.resolve(relative);
try {
Assert.assertEquals(new String(Files.readAllBytes(p), StandardCharsets.UTF_8),
new String(Files.readAllBytes(actualPath), StandardCharsets.UTF_8));
} catch (IOException e) {
throw new RuntimeException(e);
}
});

final Set<Path> unexpectedFiles = new LinkedHashSet<>();
Files.walk(actual).filter(Files::isRegularFile).forEach(p -> {
final Path relative = actual.relativize(p);
if (!expectedFiles.contains(relative)) {
unexpectedFiles.add(relative);
}
});
if (!unexpectedFiles.isEmpty()) {
Assert.fail(String.format("Files found under [%s] but not defined as expected under [%s]:%s", actual,
expected, unexpectedFiles.stream().map(Path::toString).collect(Collectors.joining("\n "))));
}
}

@Test
void getPackage() throws IOException {
Assert.assertEquals("org.apache.camel.quarkus.aws.sns.deployment", CreateExtensionMojo
.getJavaPackage("org.apache.camel.quarkus", null, "camel-quarkus-aws-sns-deployment"));
Assert.assertEquals("org.apache.camel.quarkus.component.aws.sns.deployment", CreateExtensionMojo
.getJavaPackage("org.apache.camel.quarkus", "component", "camel-quarkus-aws-sns-deployment"));
}

@Test
void toCapCamelCase() throws IOException {
Assert.assertEquals("FooBarBaz", CreateExtensionMojo.toCapCamelCase("foo-bar-baz"));
}

@Test
void addModule() throws IOException {
final Path path = Paths.get("pom.xml");
{
final String source = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" //
+ "<project xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://maven.apache.org/POM/4.0.0\"\n" //
+ " xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" //
+ " <modelVersion>4.0.0</modelVersion>\n" //
+ " <groupId>org.acme</groupId>\n" //
+ " <artifactId>grand-parent</artifactId>\n" //
+ " <version>0.1-SNAPSHOT</version>\n" //
+ " <packaging>pom</packaging>\n" //
+ "</project>\n";
final String actual = CreateExtensionMojo.addModule(path, source, "new-module");
final String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" //
+ "<project xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://maven.apache.org/POM/4.0.0\"\n" //
+ " xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" //
+ " <modelVersion>4.0.0</modelVersion>\n" //
+ " <groupId>org.acme</groupId>\n" //
+ " <artifactId>grand-parent</artifactId>\n" //
+ " <version>0.1-SNAPSHOT</version>\n" //
+ " <packaging>pom</packaging>\n" //
+ " <modules>\n" //
+ " <module>new-module</module>\n" //
+ " </modules>\n" + "</project>\n";
Assert.assertEquals(expected, actual);
}
{
final String source = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" //
+ "<project xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://maven.apache.org/POM/4.0.0\"\n" //
+ " xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" //
+ " <modelVersion>4.0.0</modelVersion>\n" //
+ " <groupId>org.acme</groupId>\n" //
+ " <artifactId>grand-parent</artifactId>\n" //
+ " <version>0.1-SNAPSHOT</version>\n" //
+ "\n" //
+ " <packaging>pom</packaging>\n" //
+ "\n" //
+ " <modules>\n" //
+ " <module>old-module</module>\n" //
+ " </modules>\n" + "</project>\n";
final String actual = CreateExtensionMojo.addModule(path, source, "new-module");
final String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" //
+ "<project xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://maven.apache.org/POM/4.0.0\"\n" //
+ " xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" //
+ " <modelVersion>4.0.0</modelVersion>\n" //
+ " <groupId>org.acme</groupId>\n" //
+ " <artifactId>grand-parent</artifactId>\n" //
+ " <version>0.1-SNAPSHOT</version>\n" //
+ "\n" //
+ " <packaging>pom</packaging>\n" //
+ "\n" //
+ " <modules>\n" //
+ " <module>old-module</module>\n" //
+ " <module>new-module</module>\n" //
+ " </modules>\n" //
+ "</project>\n";
Assert.assertEquals(expected, actual);
}
{
final String source = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" //
+ "<project xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://maven.apache.org/POM/4.0.0\"\n" //
+ " xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" //
+ " <modelVersion>4.0.0</modelVersion>\n" //
+ " <groupId>org.acme</groupId>\n" //
+ " <artifactId>grand-parent</artifactId>\n" //
+ " <version>0.1-SNAPSHOT</version>\n" //
+ " <packaging>pom</packaging>\n" //
+ "\n" //
+ " <build>\n" //
+ " </build>\n" //
+ "</project>\n";
final String actual = CreateExtensionMojo.addModule(path, source, "new-module");
final String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" //
+ "<project xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://maven.apache.org/POM/4.0.0\"\n" //
+ " xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n" //
+ " <modelVersion>4.0.0</modelVersion>\n" //
+ " <groupId>org.acme</groupId>\n" //
+ " <artifactId>grand-parent</artifactId>\n" //
+ " <version>0.1-SNAPSHOT</version>\n" //
+ " <packaging>pom</packaging>\n" //
+ "\n" //
+ " <modules>\n" //
+ " <module>new-module</module>\n" //
+ " </modules>\n" //
+ " <build>\n" //
+ " </build>\n" //
+ "</project>\n";
Assert.assertEquals(expected, actual);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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>
<parent>
<groupId>org.acme</groupId>
<artifactId>my-project-custom-grand-parent-extension-parent</artifactId>
<version>0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>my-project-custom-grand-parent-extension-deployment</artifactId>
<name>Custom Grand Parent Extension - Deployment</name>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-core-deployment</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-extension-processor</artifactId>
<version>${quarkus.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>

</project>
Loading

0 comments on commit 48c0c82

Please sign in to comment.