Skip to content
This repository has been archived by the owner on Oct 14, 2020. It is now read-only.

Commit

Permalink
Merge pull request #87 from chyt/master
Browse files Browse the repository at this point in the history
Isolate Spring Boot package logic, upgrade to liberty-maven-plugin 2.6
  • Loading branch information
chyt authored Sep 7, 2018
2 parents 38b5e80 + 9df3690 commit 8205887
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 112 deletions.
1 change: 0 additions & 1 deletion boost-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

<groupId>io.openliberty.boost</groupId>
<artifactId>boost-bom</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<description>Liberty Boost EE Feature Bom</description>
Expand Down
2 changes: 1 addition & 1 deletion boost-maven-plugin/src/it/test-docker/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
<plugin>
<groupId>io.openliberty.boost</groupId>
<artifactId>boost-maven-plugin</artifactId>
<version>0.1-SNAPSHOT</version>
<version>@pom.version@</version>
<configuration>
<repository>localhost:5000/${project.artifactId}</repository>
</configuration>
Expand Down
4 changes: 2 additions & 2 deletions boost-maven-plugin/src/it/test-jaxrs-2.0/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<dependency>
<groupId>io.openliberty.boost</groupId>
<artifactId>boost-bom</artifactId>
<version>0.1-SNAPSHOT</version>
<version>@pom.version@</version>
<scope>import</scope>
<type>pom</type>
</dependency>
Expand Down Expand Up @@ -67,7 +67,7 @@
<plugin>
<groupId>io.openliberty.boost</groupId>
<artifactId>boost-maven-plugin</artifactId>
<version>0.1-SNAPSHOT</version>
<version>@pom.version@</version>
<executions>
<execution>
<goals>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
<plugin>
<groupId>io.openliberty.boost</groupId>
<artifactId>boost-maven-plugin</artifactId>
<version>0.1-SNAPSHOT</version>
<version>@pom.version@</version>
<executions>
<execution>
<goals>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
<plugin>
<groupId>io.openliberty.boost</groupId>
<artifactId>boost-maven-plugin</artifactId>
<version>0.1-SNAPSHOT</version>
<version>@pom.version@</version>
<executions>
<execution>
<goals>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
<plugin>
<groupId>io.openliberty.boost</groupId>
<artifactId>boost-maven-plugin</artifactId>
<version>0.1-SNAPSHOT</version>
<version>@pom.version@</version>
<executions>
<execution>
<goals>
Expand Down
2 changes: 1 addition & 1 deletion boost-maven-plugin/src/it/test-websocket/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
<plugin>
<groupId>io.openliberty.boost</groupId>
<artifactId>boost-maven-plugin</artifactId>
<version>0.1-SNAPSHOT</version>
<version>@pom.version@</version>
<executions>
<execution>
<goals>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public abstract class AbstractLibertyMojo extends MojoSupport {
/**
* Version of the Liberty-Maven-Plugin used by Boost
*/
@Parameter(defaultValue = "2.6-SNAPSHOT", readonly = true)
@Parameter(defaultValue = "2.6", readonly = true)
protected String libertyMavenPluginVersion;

@Parameter(defaultValue = "${project.build.directory}", readonly = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,64 +41,75 @@

/**
* Packages an existing application into a Liberty executable jar so that the
* application can be run from the command line using java -jar. (This is for the 'jar' packaging type).
* application can be run from the command line using java -jar. (This is for
* the 'jar' packaging type).
*
*/
@Mojo(name = "package", defaultPhase = LifecyclePhase.PACKAGE, requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME)
public class LibertyPackageMojo extends AbstractLibertyMojo {

BoosterPacksParent boosterParent;
List<BoosterPackConfigurator> boosterFeatures = null;
BoosterPacksParent boosterParent;
List<BoosterPackConfigurator> boosterFeatures = null;

SpringBootUtil springBootUtil = new SpringBootUtil();

public void execute() throws MojoExecutionException {
public void execute() throws MojoExecutionException {
String springBootVersion = SpringBootProjectUtils.findSpringBootVersion(project);

boosterParent = new BoosterPacksParent();
boosterParent = new BoosterPacksParent();

createDefaultRuntimeArtifactIfNeeded();

createServer();

try {
// Copy the Spring Boot uber JAR back as the project artifact, only if Spring Boot didn't create it already
if(!springBootUtil.copySpringBootUberJar(project.getArtifact().getFile())) {
File springJar = new File(springBootUtil.getSpringBootUberJarPath(project.getArtifact().getFile()));
if(springJar.exists()) {
getLog().debug("Copying back Spring Boot Uber JAR as project artifact.");
FileUtils.copyFile(springJar, project.getArtifact().getFile());
}
if (springBootVersion != null) {
// dealing with a spring boot app
copySpringBootUberJar();
installApp("spring-boot-project");
generateServerXML();
generateBootstrapProps();
} else {
// dealing with an EE based app
installApp("project");
boosterFeatures = getBoosterConfigsFromDependencies(project);
generateServerXMLJ2EE(boosterFeatures);
}
} catch (BoostException | IOException e1) {
throw new MojoExecutionException(e1.getMessage(), e1);
}

createServer();

try {
if (springBootVersion != null) {
// dealing with a spring boot app
installApp("spring-boot-project");
generateServerXML();
generateBootstrapProps();
} else {
// dealing with an EE based app
installApp("project");
boosterFeatures = getBoosterConfigsFromDependencies(project);
generateServerXMLJ2EE(boosterFeatures);
}
} catch ( TransformerException | ParserConfigurationException e) {
} catch (TransformerException | ParserConfigurationException e) {
throw new MojoExecutionException("Unable to generate server configuration for the Liberty server", e);
}

installMissingFeatures();

createUberJar();

// Add the manifest to prevent Spring Boot from repackaging again

if (springBootVersion != null) {
// Add the manifest to prevent Spring Boot from repackaging again
try {
springBootUtil.addSpringBootVersionToManifest(project.getArtifact().getFile(), springBootVersion);
} catch (BoostException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
}

/**
* Copy the Spring Boot uber JAR back as the project artifact, only if Spring
* Boot didn't create it already
*
* @throws MojoExecutionException
*/
private void copySpringBootUberJar() throws MojoExecutionException {
try {
springBootUtil.addSpringBootVersionToManifest(project.getArtifact().getFile(), springBootVersion);
} catch (BoostException e) {
throw new MojoExecutionException(e.getMessage(), e);
if (!springBootUtil.copySpringBootUberJar(project.getArtifact().getFile())) {
File springJar = new File(springBootUtil.getSpringBootUberJarPath(project.getArtifact().getFile()));
if (springJar.exists()) {
getLog().debug("Copying back Spring Boot Uber JAR as project artifact.");
FileUtils.copyFile(springJar, project.getArtifact().getFile());
}
}
} catch (BoostException | IOException e1) {
throw new MojoExecutionException(e1.getMessage(), e1);
}
}

Expand Down Expand Up @@ -159,63 +170,65 @@ private void generateServerXML() throws TransformerException, ParserConfiguratio
LibertyServerConfigGenerator serverConfig = new LibertyServerConfigGenerator();

// Find and add appropriate springBoot features
List<String> featuresNeededForSpringBootApp = SpringBootProjectUtils.getLibertyFeaturesNeeded(project, getLog());
List<String> featuresNeededForSpringBootApp = SpringBootProjectUtils.getLibertyFeaturesNeeded(project,
getLog());
serverConfig.addFeatures(featuresNeededForSpringBootApp);

serverConfig.addHttpEndpoint(null, "${server.port}", null);

// Write server.xml to Liberty server config directory
serverConfig.writeToServer(projectBuildDir + "/liberty/wlp/usr/servers/" + libertyServerName);
}

private List<BoosterPackConfigurator> getBoosterConfigsFromDependencies(MavenProject proj) {

List<String> listOfDependencies = new ArrayList<String>();
getLog().debug("getBoostCfg: first lets see what dependencies we find");

for (Artifact artifact : project.getArtifacts()) {
getLog().debug("getBoostCfg: found this, adding as a string -> " + artifact.getGroupId() + ":" + artifact.getArtifactId());
listOfDependencies.add(artifact.getGroupId() + ":" + artifact.getArtifactId());
}

return boosterParent.mapDependenciesToFeatureList(listOfDependencies);
}

/**
* Generate a server.xml based on the found EE dependencies
*
* @throws TransformerException
* @throws ParserConfigurationException
*/
private void generateServerXMLJ2EE(List<BoosterPackConfigurator> boosterConfigurators)
throws TransformerException, ParserConfigurationException {

LibertyServerConfigGenerator serverConfig = new LibertyServerConfigGenerator();

// Add any other Liberty features needed depending on the spring boot
// starters defined
List<String> boosterFeatureNames = getBoosterFeatureNames(boosterConfigurators);
serverConfig.addFeatures(boosterFeatureNames);
serverConfig.addConfigForFeatures(boosterConfigurators);

// Write server.xml to Liberty server config directory
serverConfig.writeToServer(projectBuildDir + "/liberty/wlp/usr/servers/" + libertyServerName);

}
private List<BoosterPackConfigurator> getBoosterConfigsFromDependencies(MavenProject proj) {

List<String> listOfDependencies = new ArrayList<String>();
getLog().debug("getBoostCfg: first lets see what dependencies we find");

for (Artifact artifact : project.getArtifacts()) {
getLog().debug("getBoostCfg: found this, adding as a string -> " + artifact.getGroupId() + ":"
+ artifact.getArtifactId());
listOfDependencies.add(artifact.getGroupId() + ":" + artifact.getArtifactId());
}

return boosterParent.mapDependenciesToFeatureList(listOfDependencies);
}

/**
* Generate a server.xml based on the found EE dependencies
*
* @throws TransformerException
* @throws ParserConfigurationException
*/
private void generateServerXMLJ2EE(List<BoosterPackConfigurator> boosterConfigurators)
throws TransformerException, ParserConfigurationException {

LibertyServerConfigGenerator serverConfig = new LibertyServerConfigGenerator();

// Add any other Liberty features needed depending on the spring boot
// starters defined
List<String> boosterFeatureNames = getBoosterFeatureNames(boosterConfigurators);
serverConfig.addFeatures(boosterFeatureNames);
serverConfig.addConfigForFeatures(boosterConfigurators);

// Write server.xml to Liberty server config directory
serverConfig.writeToServer(projectBuildDir + "/liberty/wlp/usr/servers/" + libertyServerName);

}

/**
*
* @param boosterConfigurators
* @return
*/
private List<String> getBoosterFeatureNames(List<BoosterPackConfigurator> boosterConfigurators) {
List<String> featureStrings = new ArrayList<String>();
for (BoosterPackConfigurator bpconfig : boosterConfigurators){
featureStrings.add(bpconfig.getFeatureString());
}
return featureStrings;
}
List<String> featureStrings = new ArrayList<String>();
for (BoosterPackConfigurator bpconfig : boosterConfigurators) {
featureStrings.add(bpconfig.getFeatureString());
}

return featureStrings;
}

/**
* Invoke the liberty-maven-plugin to run the create-server goal
Expand All @@ -231,19 +244,13 @@ private void createServer() throws MojoExecutionException {
* Invoke the liberty-maven-plugin to run the install-app goal.
*/
private void installApp(String installAppPackagesVal) throws MojoExecutionException {

executeMojo(
getPlugin(),
goal("install-apps"),
configuration(
element(name("installAppPackages"), installAppPackagesVal),
element(name("serverName"), libertyServerName),
getRuntimeArtifactElement()
),
getExecutionEnvironment()
);

executeMojo(getPlugin(), goal("install-apps"),
configuration(element(name("installAppPackages"), installAppPackagesVal),
element(name("serverName"), libertyServerName), getRuntimeArtifactElement()),
getExecutionEnvironment());
}

/**
* Invoke the liberty-maven-plugin to run the install-feature goal.
*
Expand All @@ -254,8 +261,7 @@ private void installApp(String installAppPackagesVal) throws MojoExecutionExcept
private void installMissingFeatures() throws MojoExecutionException {

executeMojo(getPlugin(), goal("install-feature"), configuration(element(name("serverName"), libertyServerName),
element(name("features"), element(name("acceptLicense"), "true")
)), getExecutionEnvironment());
element(name("features"), element(name("acceptLicense"), "true"))), getExecutionEnvironment());
}

/**
Expand All @@ -264,13 +270,10 @@ private void installMissingFeatures() throws MojoExecutionException {
*/
private void createUberJar() throws MojoExecutionException {
// Package server into runnable jar
executeMojo(getPlugin(),
goal("package-server"), configuration(element(name("isInstall"), "false"),
element(name("include"), "minify,runnable"),
element(name("attach"), "true"),
element(name("serverName"), libertyServerName)
),
getExecutionEnvironment());
executeMojo(getPlugin(), goal("package-server"),
configuration(element(name("isInstall"), "false"), element(name("include"), "minify,runnable"),
element(name("attach"), "true"), element(name("serverName"), libertyServerName)),
getExecutionEnvironment());
}

}

0 comments on commit 8205887

Please sign in to comment.