This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Open Liberty Generator * Add unit tests and documentation for the Open Liberty generator * Add sample and integration tests for open liberty * Add license to xml files * Update changelog
- Loading branch information
1 parent
76df545
commit 724bfae
Showing
18 changed files
with
776 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
143 changes: 143 additions & 0 deletions
143
...ator/openliberty/src/main/java/io/fabric8/generator/openliberty/OpenLibertyGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
|
||
} |
1 change: 1 addition & 0 deletions
1
generator/openliberty/src/main/resources/META-INF/fabric8/generator-default
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
io.fabric8.generator.openliberty.OpenLibertyGenerator |
134 changes: 134 additions & 0 deletions
134
.../openliberty/src/test/java/io/fabric8/generator/openliberty/OpenLibertyGeneratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.