Skip to content

Commit

Permalink
Update generator plugin for adding execution to spring-boot-maven-plu…
Browse files Browse the repository at this point in the history
…gin.
  • Loading branch information
corneil committed Jun 10, 2024
1 parent 7fb4765 commit 9410a09
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ public void execute() throws MojoFailureException, MojoExecutionException {
this.global.getApplication().getFunctionDefinition() : this.application.getFunctionDefinition();
app.setFunctionDefinition(applicationFunctionDefinition); //TODO is applicationFunctionDefinition required?

app.setBootPluginConfiguration(this.application.getBootPluginConfiguration());
app.setBootExecution(this.application.getBootExecution());

String metadataMavenPluginVersion = StringUtils.isEmpty(this.application.getMetadata().getMavenPluginVersion()) ?
this.global.getApplication().getMetadata().getMavenPluginVersion() : this.application.getMetadata().getMavenPluginVersion();
if (StringUtils.isEmpty(metadataMavenPluginVersion)) {
Expand Down Expand Up @@ -306,8 +309,6 @@ else if (StringUtils.hasText(this.global.getApplication().getContainerImage().ge
})
.collect(Collectors.toList());

app.setBootPluginConfiguration(this.application.getBootPluginConfiguration());

// ----------------------------------------------------------------------------------------------------------
// Project Generator
// ----------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -465,6 +466,7 @@ public static class Application {
private final Maven maven = new Maven();

private String bootPluginConfiguration;
private String bootExecution;

public String getBootPluginConfiguration() {
return bootPluginConfiguration;
Expand All @@ -474,6 +476,15 @@ public void setBootPluginConfiguration(String bootPluginConfiguration) {
this.bootPluginConfiguration = bootPluginConfiguration;
}

public String getBootExecution() {
return bootExecution;
}

public void setBootExecution(String bootExecution) {
System.out.println(getClass() + ".bootExecution=" + bootExecution);
this.bootExecution = bootExecution;
}

public Map<String, String> getProperties() {
return properties;
}
Expand Down Expand Up @@ -631,7 +642,7 @@ public static class ContainerImage {
/**
* Base images to be used by the target container image.
*/
private String baseImage;
private String baseImage = "${project.artifactId}";

/**
* Enable or disable the inclusion of application's metadata into the image's labels.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,18 @@ public class AppDefinition {
* Application metadata related configurations to be applied for this App definition.
*/
private final Metadata metadata = new Metadata();

private String bootExecution;
private String bootPluginConfiguration;

public String getBootExecution() {
return bootExecution;
}

public void setBootExecution(String bootExecution) {
System.out.println(getClass() + ".bootExecution=" + bootExecution);
this.bootExecution = bootExecution;
}

public String getBootPluginConfiguration() {
return bootPluginConfiguration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,26 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
{{#app.bootExecution}}
<executions>
<execution>
<!-- bootExecution -->
{{this}}
</execution>
</executions>
{{/app.bootExecution}}
<configuration>
<image>
<name>{{app.containerImage.orgName}}/${project.artifactId}:{{app.containerImage.tag}}</name>
</image>
{{#app.bootPluginConfiguration}}
{{this}}
<!-- bootPluginConfiguration -->
{{this}}
{{/app.bootPluginConfiguration}}
<image>
<name>{{app.containerImage.orgName}}/{{app.containerImage.baseImage}}:{{app.containerImage.tag}}</name>
</image>
</configuration>
</plugin>
{{#app.containerImage.enableMetadata}}
<!-- enableMetadata -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
Expand Down Expand Up @@ -176,15 +186,18 @@
<version>{{app.metadata.mavenPluginVersion}}</version>
<configuration>
{{#app.containerImage.enableMetadata}}
<!-- enableMetadata -->
<storeFilteredMetadata>true</storeFilteredMetadata>
<metadataFilter>
<names>
{{#app.metadataNameFilters}}
<!-- metadataNameFilters -->
<filter>{{this}}</filter>
{{/app.metadataNameFilters}}
</names>
<sourceTypes>
{{#app.metadata.sourceTypeFilters}}
<!-- sourceTypeFilters -->
<filter>{{this}}</filter>
{{/app.metadata.sourceTypeFilters}}
</sourceTypes>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ public class MojoHarnessTest {
@Rule
public MojoRule mojoRule = new MojoRule();

// @Before
// public void before() {
// setMojoProperty("generatedProjectHome", projectHome.getRoot().getAbsolutePath());
// }
// private SpringCloudStreamAppGeneratorMojo springCloudStreamAppMojo = new SpringCloudStreamAppGeneratorMojo();
//
// private Class<? extends SpringCloudStreamAppGeneratorMojo> mojoClazz = springCloudStreamAppMojo.getClass();
// private void setMojoProperty(String propertyName, Object value) throws NoSuchFieldException {
// Field mojoProperty = mojoClazz.getDeclaredField(propertyName);
// mojoProperty.setAccessible(true);
// ReflectionUtils.setField(mojoProperty, springCloudStreamAppMojo, value);
// }
@Test
public void testSomething() throws Exception {

Expand Down Expand Up @@ -106,8 +118,14 @@ public void testSomething() throws Exception {
assertThat(springBootPlugin.getConfiguration().toString()).containsPattern("\\<image\\>\\s*" +
"\\<name\\>.+/\\$\\{project.artifactId\\}:\\d+\\.\\d+\\.\\d+.*\\</name\\>\\s*" +
"\\</image\\>");
assertThat(pomModel.getRepositories().size()).isEqualTo(5);

List<String> executions = springBootPlugin.getExecutions().stream().map(pluginExecution -> pluginExecution.getId() + ":" + pluginExecution.getPhase() + ":" + pluginExecution.getGoals()).collect(Collectors.toList());
assertThat(executions).as("Exepcted executions").isNotEmpty();
assertThat(executions.toString()).contains("process-aot");

assertThat(springBootPlugin.getConfiguration().toString()).contains("requiresUnpack");

assertThat(pomModel.getRepositories().size()).isEqualTo(5);
assertThat(pomModel.getRepositories().stream().map(r -> r.getId()).collect(Collectors.toList()))
.contains("bintray-global", "bintray-application", "bintray-binder");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void before() throws NoSuchFieldException {

// BOM
application.setBootVersion("3.3.0");
application.getMetadata().setMavenPluginVersion("1.0.2.BUILD-SNAPSHOT");
application.getMetadata().setMavenPluginVersion("1.1.0-SNAPSHOT");

setMojoProperty("application", application);

Expand Down Expand Up @@ -142,23 +142,27 @@ public void testDefaultProjectCreationByPlugin() throws Exception {
}

@Test
public void testCustomBootMavenPluginConfiguration() throws Exception {
application.setBootPluginConfiguration("<![CDATA[\n" +
" <requiresUnpack>\n" +
" <dependency>\n" +
" <groupId>org.python</groupId>\n" +
" <artifactId>jython-standalone</artifactId>\n" +
" </dependency>\n" +
" </requiresUnpack>\n" +
" ]]>");

public void testCustomBootMavenExecutionsAndPluginConfiguration() throws Exception {
application.setBootPluginConfiguration("<requiresUnpack>" +
"<dependency>" +
"<groupId>org.python</groupId>" +
"<artifactId>jython-standalone</artifactId>" +
"</dependency>" +
"</requiresUnpack>");
application.setBootExecution("<id>process-aot</id>" +
"<goals>" +
"<goal>process-aot</goal>" +
"</goals>");
springCloudStreamAppMojo.execute();

Model pomModel = getModel(new File(projectHome.getRoot().getAbsolutePath()));
List<Plugin> plugins = pomModel.getBuild().getPlugins();
final Optional<Plugin> bootPlugin = plugins.stream().filter(p -> p.getArtifactId().equals("spring-boot-maven-plugin")).findFirst();
assertThat(bootPlugin.isPresent()).isTrue();

assertThat(bootPlugin.get().getConfiguration().toString()).contains("requiresUnpack");
assertThat(bootPlugin.get().getExecutions()).as("Expected executions").isNotEmpty();
assertThat(bootPlugin.get().getExecutions().get(0).getGoals()).as("Expected goals").isNotEmpty();
assertThat(bootPlugin.get().getExecutions().get(0).getGoals().get(0)).isEqualTo("process-aot");
}

private void assertGeneratedPomXml(File rootPath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,24 @@
<version>${project.version}</version>
<configClass>io.pivotal.java.function.http.supplier.HttpSupplierConfiguration.class</configClass>
<groupId>org.springframework.cloud.stream.app.test</groupId>

<bootExecution><![CDATA[
<!-- http-source-apps -->
<id>process-aot</id>
<goals>
<goal>process-aot</goal>
</goals>
]]>
</bootExecution>
<bootPluginConfiguration><![CDATA[
<!-- http-source-apps -->
<requiresUnpack>
<dependency>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
</dependency>
</requiresUnpack>
]]>
</bootPluginConfiguration>
<properties>
<spring.main.web-application-type>reactive</spring.main.web-application-type>
<spring.cloud.streamapp.security.enabled>false</spring.cloud.streamapp.security.enabled>
Expand Down

0 comments on commit 9410a09

Please sign in to comment.