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

Dev/Release project support for Gradle #282

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions boost-gradle/gradle-test.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
export testExitCode=0

./gradlew clean install check -Ptest.exclude="**/*15*" -Druntime=$RUNTIME -DruntimeVersion=$RUNTIME_VERSION --stacktrace --info --no-daemon
if [ $? ]; then
testExitCode=1
fi

ls build/test-results/test/ >> out.txt
./gradlew wrapper --gradle-version 4.10

./gradlew clean install check -Ptest.include="**/*15*" -Druntime=$RUNTIME -DruntimeVersion=$RUNTIME_VERSION --stacktrace --info --no-daemon
if [ $? ]; then
testExitCode=1
fi

ls build/test-results/test/ >> out.txt
cat out.txt

exit $testExitCode
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ public class Boost implements Plugin<Project> {

void apply(Project project) {
project.extensions.create('boost', BoostExtension)
project.configurations.create('boostApp')

BoostLogger.init(project)

new BoostTaskFactory(project).createTasks()

project.pluginManager.apply('net.wasdev.wlp.gradle.plugins.Liberty')
project.configurations.libertyApp.extendsFrom(project.configurations.boostApp)

project.liberty.server = configureBoostServerProperties()
configureRuntimeArtifact(project)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import java.io.FileOutputStream
import java.io.IOException

import org.apache.commons.io.FileUtils
import org.apache.commons.io.FilenameUtils

import javax.xml.parsers.ParserConfigurationException
import javax.xml.transform.TransformerException
Expand Down Expand Up @@ -134,7 +135,7 @@ public class BoostPackageTask extends AbstractBoostTask {
copySpringBootUberJar(springBootUberJar)
generateServerConfigSpringBoot()

} else if (project.plugins.hasPlugin('war')) {
} else if (project.plugins.hasPlugin('war') || !project.configurations.boostApp.isEmpty()) {
// Get booster dependencies from project
Map<String, String> dependencies = GradleProjectUtil.getAllDependencies(project, BoostLogger.getInstance())

Expand Down Expand Up @@ -214,20 +215,22 @@ public class BoostPackageTask extends AbstractBoostTask {
}

protected void generateServerConfigEE() throws GradleException {
String warName = null
List<String> warNames = new ArrayList<String>()

if (project.war != null) {
if (project.plugins.hasPlugin('war')) {

if (project.war.version == null) {
warName = project.war.baseName
warNames.add(project.war.baseName)
} else {
warName = project.war.baseName + "-" + project.war.version
warNames.add(project.war.baseName + "-" + project.war.version)
}
} else {
warNames = getWarNameFromBoostApps()
}

try {

BoosterConfigurator.generateLibertyServerConfig(libertyServerPath, boosterPackConfigurators, Arrays.asList(warName), BoostLogger.getInstance());
BoosterConfigurator.generateLibertyServerConfig(libertyServerPath, boosterPackConfigurators, warNames, BoostLogger.getInstance());

} catch (Exception e) {
throw new GradleException("Unable to generate server configuration for the Liberty server.", e);
Expand Down Expand Up @@ -317,4 +320,14 @@ public class BoostPackageTask extends AbstractBoostTask {
}
}

//Runs through the dependencies in the boostApp configuration and pulls out the first war name.
protected List<String> getWarNameFromBoostApps() {
List<String> warNames = new ArrayList<String>()
for (def dep : project.configurations.boostApp) {
if (FilenameUtils.getExtension(dep.name).equals('war')) {
warNames.add(dep.name.substring(0, dep.name.length() - 4))
}
}
return warNames
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
package io.openliberty.boost.gradle.utils

import org.gradle.api.Project
import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.ModuleVersionIdentifier
import org.gradle.api.artifacts.UnknownConfigurationException
import org.gradle.maven.MavenModule
import org.gradle.maven.MavenPomArtifact
import org.gradle.api.artifacts.ModuleVersionIdentifier

import groovy.lang.MissingPropertyException

Expand Down Expand Up @@ -45,13 +47,36 @@ public class GradleProjectUtil {
Map<String, String> dependencies = new HashMap<String, String>()
logger.debug("Processing project for dependencies.")

project.configurations.compileClasspath.resolvedConfiguration.resolvedArtifacts.collect { it.moduleVersion.id }.each { ModuleVersionIdentifier id ->
try {
//Projects without the war/java plugin won't have this configuration
//compileClasspath is not a regular Configuration object so we have to go through without using getAllDependenciesFromConfiguration()
project.configurations.getByName('compileClasspath').resolvedConfiguration.resolvedArtifacts.collect { it.moduleVersion.id }.each { ModuleVersionIdentifier id ->
logger.debug("Found dependency while processing project: " + id.group.toString() + ":"
+ id.name.toString() + ":" + id.version.toString())

dependencies.put(id.group.toString() + ":" + id.name.toString(), id.version.toString())
}
} catch (UnknownConfigurationException ue) {
logger.debug("The compileClasspath configuration was not found.")
}

//Will always have the boostApp configuration since we create it in apply()
//Just pulling in the transitive booster dependencies for the apps
dependencies.putAll(getAllBoosterDependenciesFromConfiguration(project.configurations.boostApp, logger))

return dependencies
}

private static Map<String, String> getAllBoosterDependenciesFromConfiguration(Configuration configuration, BoostLogger logger) {
Map<String, String> dependencies = new HashMap<String, String>()
configuration.resolvedConfiguration.resolvedArtifacts.collect { it.moduleVersion.id }.each { ModuleVersionIdentifier id ->
logger.debug("Found dependency while processing project: " + id.group.toString() + ":"
+ id.name.toString() + ":" + id.version.toString())

dependencies.put(id.group.toString() + ":" + id.name.toString(), id.version.toString())
if (id.group.toString().equals('io.openliberty.boosters')) {
dependencies.put(id.group.toString() + ":" + id.name.toString(), id.version.toString())
}
}

return dependencies
}
}
84 changes: 84 additions & 0 deletions boost-gradle/src/test/groovy/BoostPackageDevReleaseTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*******************************************************************************
* Copyright (c) 2019 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
import org.apache.commons.io.FileUtils

import org.gradle.testkit.runner.BuildResult
import org.gradle.testkit.runner.GradleRunner
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS

import org.junit.After
import org.junit.BeforeClass
import org.junit.Test
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertTrue


public class BoostPackageDevReleaseTest extends AbstractBoostTest {
static File resourceDir = new File("build/resources/test/devReleaseApp")
static File testProjectDir = new File(integTestDir, "BoostPackageDevReleaseTest")

static File devDir = new File(testProjectDir, 'dev')
static File releaseDir = new File(testProjectDir, 'release')

private static String URL = "http://localhost:9080/"

private static String SERVLET_RESPONSE = "</font><font color=red>myHomeCounty</font></h1>"

@BeforeClass
public static void setup() {
createDir(testProjectDir)
FileUtils.copyDirectory(resourceDir, testProjectDir)
copyFile(new File("build/gradle.properties"), new File(devDir, 'gradle.properties'))
copyFile(new File("build/gradle.properties"), new File(releaseDir, 'gradle.properties'))

//Build the dev project
BuildResult result = GradleRunner.create()
.withProjectDir(devDir)
.forwardOutput()
.withArguments("install", "-i", "-s")
.build()

assertEquals(SUCCESS, result.task(":boostPackage").getOutcome())

//Build the release project
result = GradleRunner.create()
.withProjectDir(releaseDir)
.forwardOutput()
.withArguments("boostPackage", "boostStart", "-i", "-s")
.build()

assertEquals(SUCCESS, result.task(":boostPackage").getOutcome())
assertEquals(SUCCESS, result.task(":boostStart").getOutcome())
}

@After
public void teardown() {

BuildResult result = GradleRunner.create()
.withProjectDir(releaseDir)
.forwardOutput()
.withArguments("boostStop", "-i", "-s")
.build()

assertEquals(SUCCESS, result.task(":boostStop").getOutcome())

}

@Test
public void checkForApplication() {
assertTrue(new File(releaseDir, 'build/wlp/usr/servers/BoostServer/apps/app-1.0.war').exists())
}

@Test
public void testServletResponse() throws Exception {
testServlet(URL, SERVLET_RESPONSE)
}
}
50 changes: 50 additions & 0 deletions boost-gradle/src/test/resources/devReleaseApp/dev/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
buildscript {
repositories {
mavenCentral()
mavenLocal()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
maven { url 'https://repo.spring.io/plugins-snapshot' }
}
dependencies {
classpath("io.openliberty.boost:boost-gradle-plugin:${boostVersion}")
classpath 'io.spring.gradle:dependency-management-plugin:1.0.6.RELEASE'
}
}

apply plugin: 'war'
apply plugin: 'maven'
apply plugin: 'boost'
apply plugin: 'io.spring.dependency-management'

group = "test"
version = "1.0"

war {
baseName = 'app'
}

repositories {
mavenCentral()
mavenLocal()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencyManagement {
imports {
mavenBom "io.openliberty.boosters:ee8-bom:${boosterVersion}"
}
}

dependencies {
compile "io.openliberty.boosters:jaxrs"
compile "io.openliberty.boosters:jdbc"
compile("org.apache.httpcomponents:httpclient:4.5.6")
compile("org.apache.httpcomponents:httpcore:4.4.10")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix indent

compile "javax.servlet:javax.servlet-api:4.0.0"

libertyRuntime "$runtimeGroup:$runtimeArtifactId:$runtimeVersion"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package application.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

/**
* Servlet implementation class JdbcServlet
*/
@WebServlet("/*")
public class JdbcServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
@Resource
DataSource ds1;

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Statement stmt = null;
Connection con = null;

try {
con = ds1.getConnection();

stmt = con.createStatement();
// create a table
stmt.executeUpdate(
"create table cities (name varchar(50) not null primary key, population int, county varchar(30))");
// insert a test record
stmt.executeUpdate("insert into cities values ('myHomeCity', 106769, 'myHomeCounty')");
// select a record
ResultSet result = stmt.executeQuery("select county from cities where name='myHomeCity'");
result.next();
// display the county information for the city.
response.getWriter().print("<h1><font color=green>Text retrieved from database is: </font>"
+ "<font color=red>" + result.getString(1) + "</font></h1>");
// System.out.println("The county for myHomeCity is " +
// result.getString(1));
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
// drop the table to clean up and to be able to rerun the test.
stmt.executeUpdate("drop table cities");
} catch (SQLException e) {
e.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>Java REST Sample</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Binary file not shown.
24 changes: 24 additions & 0 deletions boost-gradle/src/test/resources/devReleaseApp/release/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
buildscript {
repositories {
mavenCentral()
mavenLocal()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
maven { url 'https://repo.spring.io/plugins-snapshot' }
}
dependencies {
classpath("io.openliberty.boost:boost-gradle-plugin:${boostVersion}")
}
}

apply plugin: 'boost'

repositories {
mavenCentral()
mavenLocal()
}

dependencies {
boostApp "test:app:1.0"
}