Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Add Open Liberty Generator #1711

Merged
merged 5 commits into from
Oct 1, 2019
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ After this we will switch probably to real [Semantic Versioning 2.0.0](http://se
* Feature #1536: Java Image Builder Support
* Fix #1704: fabric8-build failing on openshift
* Feature #1706: Prometheus Enricher; Configuration support for Prometheus path
* Added generator support for Open Liberty

### 4.2.0 (01-08-2019)
* Fix #1638: Remove enrichAll parameter from ImageChangeTriggerEnricher
Expand Down
5 changes: 5 additions & 0 deletions doc/src/main/asciidoc/inc/_generator.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ All default generators examine the build information for certain aspects and gen
| <<generator-webapp,Web applications>>
| `webapps`
| Generator for WAR based applications supporting Tomcat, Jetty and Wildfly base images

| <<generator-openliberty,Open Liberty>>
| `webapps`
| Generator for Open Liberty applications
|===

include::generator/_options_common.adoc[]
Expand All @@ -52,5 +56,6 @@ include::generator/_thorntail_v2.adoc[]
include::generator/_vertx.adoc[]
include::generator/_karaf.adoc[]
include::generator/_webapp.adoc[]
include::generator/_openliberty.adoc[]

include::generator/_api.adoc[]
8 changes: 8 additions & 0 deletions doc/src/main/asciidoc/inc/generator/_openliberty.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[[generator-openliberty]]
=== Open Liberty

The Open Liberty generator runs when the Open Liberty plugin is enabled in the maven build.

The generator is similar to the <<generator-java-exec,java-exec generator>>. It supports the <<generator-options-common, common generator options>> and the <<generator-java-exec-options, `java-exec` options>>.

For Open Liberty, the default value of webPort is 9080.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public ExposeEnricher(MavenEnricherContext buildContext) {
super(buildContext, "fmp-openshift-service-expose");
}

private Set<Integer> webPorts = new HashSet<>(Arrays.asList(80, 443, 8080, 9090));
private Set<Integer> webPorts = new HashSet<>(Arrays.asList(80, 443, 8080, 9080, 9090, 9443));

public static final String EXPOSE_LABEL = "expose";

Expand Down
59 changes: 59 additions & 0 deletions generator/openliberty/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright 2016 Red Hat, Inc.

Red Hat licenses this file to you under the Apache License, version
2.0 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.

-->
<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>

<parent>
<groupId>io.fabric8</groupId>
<artifactId>fabric8-maven-plugin-parent</artifactId>
<version>4.3-SNAPSHOT</version>
<relativePath>../../parent/pom.xml</relativePath>
</parent>

<artifactId>fabric8-maven-plugin-generator-openliberty</artifactId>
<version>4.3-SNAPSHOT</version>

<name>Fabric8 Maven :: Generator :: Open Liberty</name>

<dependencies>
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>fabric8-maven-plugin-generator-api</artifactId>
</dependency>

<dependency>
<groupId>io.fabric8</groupId>
<artifactId>fabric8-maven-plugin-generator-java-exec</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/**
* Copyright 2016 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version
* 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package io.fabric8.generator.openliberty;

import static io.fabric8.maven.core.util.FileUtil.getRelativePath;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.assembly.model.Assembly;
import org.apache.maven.plugins.assembly.model.DependencySet;
import org.apache.maven.plugins.assembly.model.FileSet;
import org.apache.maven.project.MavenProject;

import io.fabric8.maven.core.util.FatJarDetector;
import io.fabric8.maven.core.util.MavenUtil;
import io.fabric8.maven.docker.config.AssemblyConfiguration;
import io.fabric8.maven.docker.config.ImageConfiguration;
import io.fabric8.maven.generator.api.GeneratorContext;
import io.fabric8.maven.generator.javaexec.JavaExecGenerator;

public class OpenLibertyGenerator extends JavaExecGenerator {

protected static final String LIBERTY_SELF_EXTRACTOR = "wlp.lib.extract.SelfExtractRun";
protected static final String LIBERTY_RUNNABLE_JAR = "LIBERTY_RUNNABLE_JAR";
protected static final String JAVA_APP_JAR = "JAVA_APP_JAR";

private String runnableJarName = null;

public OpenLibertyGenerator(GeneratorContext context) {
super(context, "openliberty");
}

// Override so that the generator kicks in when the liberty-maven-plugin is used
@Override
public boolean isApplicable(List<ImageConfiguration> configs) {
return shouldAddImageConfiguration(configs)
&& MavenUtil.hasPlugin(getProject(), "io.openliberty.tools", "liberty-maven-plugin");

}

// Override extractPorts so that we default to 9080 rather than 8080 for the web port.
@Override
protected List<String> extractPorts() {
List<String> ret = new ArrayList<>();
addPortIfValid(ret, getConfig(JavaExecGenerator.Config.webPort, "9080"));
addPortIfValid(ret, getConfig(JavaExecGenerator.Config.jolokiaPort));
addPortIfValid(ret, getConfig(JavaExecGenerator.Config.prometheusPort));
return ret;
}


@Override
protected Map<String, String> getEnv(boolean prePackagePhase) throws MojoExecutionException {
Map<String,String> ret = super.getEnv(prePackagePhase);
if ( runnableJarName != null) {
ret.put(LIBERTY_RUNNABLE_JAR, runnableJarName);
ret.put(JAVA_APP_JAR, runnableJarName);
}
return ret;
}
@Override
protected AssemblyConfiguration createAssembly() throws MojoExecutionException {
AssemblyConfiguration.Builder builder = new AssemblyConfiguration.Builder().targetDir(getConfig(Config.targetDir));
addAssembly(builder);
return builder.build();
}

@Override
protected void addAssembly(AssemblyConfiguration.Builder builder) throws MojoExecutionException {
String assemblyRef = getConfig(Config.assemblyRef);
if (assemblyRef != null) {
builder.descriptorRef(assemblyRef);
} else {
Assembly assembly = new Assembly();
addAdditionalFiles(assembly);
if (isFatJar()) {
FatJarDetector.Result fatJar = detectFatJar();
MavenProject project = getProject();
if (fatJar == null) {
DependencySet dependencySet = new DependencySet();
dependencySet.addInclude(project.getGroupId() + ":" + project.getArtifactId());
assembly.addDependencySet(dependencySet);
} else {
FileSet fileSet = getOutputDirectoryFileSet(fatJar, project);
if ( LIBERTY_SELF_EXTRACTOR.equals(fatJar.getMainClass())) {
this.runnableJarName = fatJar.getArchiveFile().getName();
}
assembly.addFileSet(fileSet);
}
} else {
builder.descriptorRef("artifact-with-dependencies");
}
builder.assemblyDef(assembly);
}
}

private void addAdditionalFiles(Assembly assembly) {
assembly.addFileSet(createFileSet("src/main/fabric8-includes/bin","bin","0755","0755"));
assembly.addFileSet(createFileSet("src/main/fabric8-includes",".","0644","0755"));
// Add server.xml file
assembly.addFileSet(createFileSet("src/main/liberty/config","src/wlp/config","0644", "0755"));

}

private FileSet getOutputDirectoryFileSet(FatJarDetector.Result fatJar, MavenProject project) {
FileSet fileSet = new FileSet();
File buildDir = new File(project.getBuild().getDirectory());
fileSet.setDirectory(getRelativePath(project.getBasedir(), buildDir).getPath());
fileSet.addInclude(getRelativePath(buildDir, fatJar.getArchiveFile()).getPath());
fileSet.setOutputDirectory(".");
fileSet.setFileMode("0640");
return fileSet;
}

private FileSet createFileSet(String sourceDir, String outputDir, String fileMode, String directoryMode) {
FileSet fileSet = new FileSet();
fileSet.setDirectory(sourceDir);
fileSet.setOutputDirectory(outputDir);
fileSet.setFileMode(fileMode);
fileSet.setDirectoryMode(directoryMode);
return fileSet;
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.fabric8.generator.openliberty.OpenLibertyGenerator
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/**
* Copyright 2016 Red Hat, Inc.
*
* Red Hat licenses this file to you under the Apache License, version
* 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package io.fabric8.generator.openliberty;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.jar.Attributes;

import org.apache.maven.model.Build;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.junit.Test;

import io.fabric8.maven.core.util.FatJarDetector;
import io.fabric8.maven.docker.config.AssemblyConfiguration;
import io.fabric8.maven.docker.util.Logger;
import io.fabric8.maven.generator.api.GeneratorContext;
import mockit.Expectations;
import mockit.Invocation;
import mockit.Mock;
import mockit.MockUp;
import mockit.Mocked;

public class OpenLibertyGeneratorTest {

@Mocked
Logger log;
@Mocked
private GeneratorContext context;

@Mocked
private MavenProject project;

@Mocked
private Build build;

@Test
public void testLibertyRunnable() throws MojoExecutionException, IOException {

new MockFatJarDetector(true);

OpenLibertyGenerator generator = new OpenLibertyGenerator(createGeneratorContext());

System.out.println("gen: " + generator);
generator.addAssembly(new AssemblyConfiguration.Builder());
assertTrue("The LIBERTY_RUNNABLE_JAR env var should be set",
generator.getEnv(false).containsKey(OpenLibertyGenerator.LIBERTY_RUNNABLE_JAR));
assertTrue("The JAVA_APP_DIR env var should be set",
generator.getEnv(false).containsKey(OpenLibertyGenerator.JAVA_APP_JAR));

}

@Test
public void testExtractPorts() throws IOException, MojoExecutionException {

OpenLibertyGenerator generator = new OpenLibertyGenerator(createGeneratorContext());
List<String> ports = generator.extractPorts();
assertNotNull(ports);
assertTrue("The list of ports should contain 9080", ports.contains("9080"));

}

private GeneratorContext createGeneratorContext() throws IOException {
new Expectations() {
{
context.getProject();
result = project;
project.getBuild();
result = build;
project.getBasedir();
minTimes = 0;
result = "basedirectory";

String tempDir = Files.createTempDirectory("openliberty-test-project").toFile().getAbsolutePath();

build.getDirectory();
result = tempDir;
build.getOutputDirectory();
result = tempDir;
project.getPlugin(anyString);
result = null;
minTimes = 0;
project.getBuildPlugins();
result = null;
project.getVersion();
result = "1.0.0";
minTimes = 0;
}
};
return context;

}

public static class MockFatJarDetector extends MockUp<FatJarDetector> {

private final boolean findClass;

public MockFatJarDetector(boolean findClass) {
this.findClass = findClass;
}

@Mock
FatJarDetector.Result scan(Invocation invocation) {

if (!findClass) {
return null;
}

FatJarDetector detector = invocation.getInvokedInstance();
return detector.new Result(new File("/the/archive/file"), OpenLibertyGenerator.LIBERTY_SELF_EXTRACTOR,
new Attributes());
}

}

}
6 changes: 6 additions & 0 deletions parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.fabric8</groupId>
<artifactId>fabric8-maven-plugin-generator-openliberty</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.fabric8</groupId>
<artifactId>fabric8-maven-plugin-generator-webapp</artifactId>
Expand Down
Loading