diff --git a/boost-common/src/main/java/io/openliberty/boost/common/config/BoosterConfigurator.java b/boost-common/src/main/java/io/openliberty/boost/common/config/BoosterConfigurator.java index eb364159..c8482171 100644 --- a/boost-common/src/main/java/io/openliberty/boost/common/config/BoosterConfigurator.java +++ b/boost-common/src/main/java/io/openliberty/boost/common/config/BoosterConfigurator.java @@ -29,6 +29,7 @@ public class BoosterConfigurator { * take a list of pom boost dependency strings and map to liberty features * for config. return a list of feature configuration objects for each found * dependency. + * * @param dependencies * @param logger * @return @@ -41,31 +42,33 @@ public class BoosterConfigurator { * @throws SecurityException */ public static List getBoosterPackConfigurators(Map dependencies, - BoostLoggerI logger) throws BoostException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { + BoostLoggerI logger) throws BoostException, InstantiationException, IllegalAccessException, + IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { List boosterPackConfigList = new ArrayList(); - + Reflections reflections = new Reflections("io.openliberty.boost.common.boosters"); Set> allClasses = reflections.getSubTypesOf(AbstractBoosterConfig.class); - for(Class boosterClass : allClasses) { + for (Class boosterClass : allClasses) { if (dependencies.containsKey(AbstractBoosterConfig.getCoordindates(boosterClass))) { Constructor cons = boosterClass.getConstructor(Map.class, BoostLoggerI.class); Object o = cons.newInstance(dependencies, logger); - if(o instanceof AbstractBoosterConfig) { - boosterPackConfigList.add((AbstractBoosterConfig)o); - } - else { - throw new BoostException("Found a class in io.openliberty.boost.common.boosters that did not extend AbstractBoosterConfig. This should never happen."); + if (o instanceof AbstractBoosterConfig) { + boosterPackConfigList.add((AbstractBoosterConfig) o); + } else { + throw new BoostException( + "Found a class in io.openliberty.boost.common.boosters that did not extend AbstractBoosterConfig. This should never happen."); } } } - + return boosterPackConfigList; } public static void generateLibertyServerConfig(String libertyServerPath, - List boosterPackConfigurators, String warName, BoostLoggerI logger) throws Exception { + List boosterPackConfigurators, List warNames, BoostLoggerI logger) + throws Exception { LibertyServerConfigGenerator serverConfig = new LibertyServerConfigGenerator(libertyServerPath, logger); @@ -77,11 +80,13 @@ public static void generateLibertyServerConfig(String libertyServerPath, } // Add war configuration is necessary - if (warName != null) { - serverConfig.addApplication(warName); + if (!warNames.isEmpty()) { + for (String warName : warNames) { + serverConfig.addApplication(warName); + } } else { throw new Exception( - "Unsupported packaging type - Liberty Boost currently supports WAR packaging type only."); + "No war files were found. The project must have a war packaging type or specify war dependencies."); } serverConfig.writeToServer(); diff --git a/boost-common/src/main/java/io/openliberty/boost/common/config/ConfigConstants.java b/boost-common/src/main/java/io/openliberty/boost/common/config/ConfigConstants.java index b07ace6d..676c70c6 100644 --- a/boost-common/src/main/java/io/openliberty/boost/common/config/ConfigConstants.java +++ b/boost-common/src/main/java/io/openliberty/boost/common/config/ConfigConstants.java @@ -20,8 +20,9 @@ public final class ConfigConstants { public static final String VERSION = "version"; public static final String WAR_PKG_TYPE = "war"; - public static final String SPRING_BOOT_PROJ = "spring-boot-project"; - public static final String NORMAL_PROJ = "project"; + public static final String INSTALL_PACKAGE_SPRING = "spring-boot-project"; + public static final String INSTALL_PACKAGE_ALL= "all"; + public static final String INSTALL_PACKAGE_DEP= "dependencies"; public static final String FEATURE_MANAGER = "featureManager"; public static final String HTTP_ENDPOINT = "httpEndpoint"; diff --git a/boost-maven/boost-maven-plugin/pom.xml b/boost-maven/boost-maven-plugin/pom.xml index cecf48c7..88a1d878 100644 --- a/boost-maven/boost-maven-plugin/pom.xml +++ b/boost-maven/boost-maven-plugin/pom.xml @@ -52,6 +52,11 @@ docker-client 8.11.7 + + org.eclipse.aether + aether-api + 0.9.0.M2 + org.codehaus.mojo plugin-support diff --git a/boost-maven/boost-maven-plugin/src/it/test-dev-release/pom.xml b/boost-maven/boost-maven-plugin/src/it/test-dev-release/pom.xml new file mode 100644 index 00000000..2be8fc0e --- /dev/null +++ b/boost-maven/boost-maven-plugin/src/it/test-dev-release/pom.xml @@ -0,0 +1,55 @@ + + + + 4.0.0 + + io.openliberty.boost.test + test-dev-release + @pom.version@ + + + UTF-8 + UTF-8 + 1.8 + 1.8 + + + + + + maven-invoker-plugin + 3.2.0 + + + install + + src/it + ${project.build.directory}/it + true + + dev-project/pom.xml + + + */pom.xml + + + ${runtimeGroupId} + ${runtimeArtifactId} + ${runtimeVersion} + + + + + integration-test + + run + + + + + + + + diff --git a/boost-maven/boost-maven-plugin/src/it/test-dev-release/src/it/dev-project/pom.xml b/boost-maven/boost-maven-plugin/src/it/test-dev-release/src/it/dev-project/pom.xml new file mode 100644 index 00000000..2b97c6bf --- /dev/null +++ b/boost-maven/boost-maven-plugin/src/it/test-dev-release/src/it/dev-project/pom.xml @@ -0,0 +1,95 @@ + + 4.0.0 + + io.openliberty.boost + dev-project + war + 1.0-SNAPSHOT + + + UTF-8 + UTF-8 + 1.8 + 1.8 + + + + + + sonatype-nexus-snapshots + Sonatype Nexus Snapshots + https://oss.sonatype.org/content/repositories/snapshots/ + + true + + + false + + + + + + + + io.openliberty.boosters + ee8-bom + @pom.version@ + import + pom + + + io.openliberty.boosters + mp20-bom + @pom.version@ + import + pom + + + + + + + io.openliberty.boosters + jdbc + + + io.openliberty.boosters + jaxrs + + + io.openliberty.boosters + mpHealth + + + io.openliberty.boosters + cdi + + + + + + + + io.openliberty.boost + boost-maven-plugin + @pom.version@ + + + ${runtimeGroupId} + ${runtimeArtifactId} + ${runtimeVersion} + zip + + + + + + package + + + + + + + diff --git a/boost-maven/boost-maven-plugin/src/it/test-dev-release/src/it/dev-project/src/main/java/application/PopulationResource.java b/boost-maven/boost-maven-plugin/src/it/test-dev-release/src/it/dev-project/src/main/java/application/PopulationResource.java new file mode 100644 index 00000000..4f8a99b3 --- /dev/null +++ b/boost-maven/boost-maven-plugin/src/it/test-dev-release/src/it/dev-project/src/main/java/application/PopulationResource.java @@ -0,0 +1,68 @@ +package application; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import javax.enterprise.context.ApplicationScoped; +import javax.annotation.Resource; +import javax.sql.DataSource; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; + +/** + * Servlet implementation class JdbcServlet + */ +@Path("/population") +@ApplicationScoped +public class PopulationResource { + + @Resource + DataSource ds1; + + @GET + @Produces("text/plain") + public String getInformation() throws Exception, IOException { + + String returnValue = null; + 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 ('New York City', 8550405, 'Unitest States')"); + // select a record + ResultSet result = stmt.executeQuery("select population from cities where name='New York City'"); + result.next(); + + returnValue = "The population of NYC 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(); + } + } + + return returnValue; + } + +} diff --git a/boost-maven/boost-maven-plugin/src/it/test-dev-release/src/it/dev-project/src/main/webapp/WEB-INF/web.xml b/boost-maven/boost-maven-plugin/src/it/test-dev-release/src/it/dev-project/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 00000000..2bb59aa8 --- /dev/null +++ b/boost-maven/boost-maven-plugin/src/it/test-dev-release/src/it/dev-project/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,15 @@ + + + Java REST Sample + + javax.ws.rs.core.Application + 1 + + + javax.ws.rs.core.Application + /* + + + index.html + + diff --git a/boost-maven/boost-maven-plugin/src/it/test-dev-release/src/it/dev-project/src/main/webapp/index.html b/boost-maven/boost-maven-plugin/src/it/test-dev-release/src/it/dev-project/src/main/webapp/index.html new file mode 100644 index 00000000..e3a3a3ff Binary files /dev/null and b/boost-maven/boost-maven-plugin/src/it/test-dev-release/src/it/dev-project/src/main/webapp/index.html differ diff --git a/boost-maven/boost-maven-plugin/src/it/test-dev-release/src/it/release-project/pom.xml b/boost-maven/boost-maven-plugin/src/it/test-dev-release/src/it/release-project/pom.xml new file mode 100644 index 00000000..264eb055 --- /dev/null +++ b/boost-maven/boost-maven-plugin/src/it/test-dev-release/src/it/release-project/pom.xml @@ -0,0 +1,110 @@ + + 4.0.0 + + io.openliberty.boost + release-project + jar + 1.0-SNAPSHOT + + + UTF-8 + UTF-8 + 1.8 + 1.8 + + + + + + sonatype-nexus-snapshots + Sonatype Nexus Snapshots + https://oss.sonatype.org/content/repositories/snapshots/ + + true + + + false + + + + + + + io.openliberty.boost + dev-project + war + 1.0-SNAPSHOT + + + + org.apache.derby + derby + 10.11.1.1 + + + junit + junit + 4.12 + test + + + commons-httpclient + commons-httpclient + 3.1 + test + + + + + + + io.openliberty.boost + boost-maven-plugin + @pom.version@ + + + ${runtimeGroupId} + ${runtimeArtifactId} + ${runtimeVersion} + zip + + + + + + package + + + + test-start-server + pre-integration-test + + start + + + + test-stop-server + post-integration-test + + stop + + + + + + org.apache.maven.plugins + maven-failsafe-plugin + 2.19.1 + + + + integration-test + verify + + + + + + + diff --git a/boost-maven/boost-maven-plugin/src/it/test-dev-release/src/it/release-project/src/test/java/it/DerbyVersionIT.java b/boost-maven/boost-maven-plugin/src/it/test-dev-release/src/it/release-project/src/test/java/it/DerbyVersionIT.java new file mode 100644 index 00000000..9f495cf5 --- /dev/null +++ b/boost-maven/boost-maven-plugin/src/it/test-dev-release/src/it/release-project/src/test/java/it/DerbyVersionIT.java @@ -0,0 +1,36 @@ +/******************************************************************************* + * Copyright (c) 2018 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 + *******************************************************************************/ +package it; + +import static org.junit.Assert.*; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.junit.Test; + +public class DerbyVersionIT { + + private static final String DERBY_JAR = "target/liberty/wlp/usr/servers/BoostServer/resources/derby-10.11.1.1.jar"; + + /** + * Test that the release project is using the Derby version that it defined + * in its own project + * + * @throws Exception + */ + @Test + public void testDerbyVersion() throws Exception { + File targetFile = new File(DERBY_JAR); + assertTrue(targetFile.getCanonicalFile() + "does not exist.", targetFile.exists()); + } +} diff --git a/boost-maven/boost-maven-plugin/src/it/test-dev-release/src/it/release-project/src/test/java/it/EndpointIT.java b/boost-maven/boost-maven-plugin/src/it/test-dev-release/src/it/release-project/src/test/java/it/EndpointIT.java new file mode 100644 index 00000000..e2c3fc90 --- /dev/null +++ b/boost-maven/boost-maven-plugin/src/it/test-dev-release/src/it/release-project/src/test/java/it/EndpointIT.java @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (c) 2018 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 + *******************************************************************************/ +package it; + +import static org.junit.Assert.*; +import org.junit.BeforeClass; +import org.junit.Test; +import org.apache.commons.httpclient.HttpClient; +import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.methods.GetMethod; + +public class EndpointIT { + private static String URL; + + @BeforeClass + public static void init() { + URL = "http://localhost:9080/population"; + } + + @Test + public void testServlet() throws Exception { + HttpClient client = new HttpClient(); + + GetMethod method = new GetMethod(URL); + + try { + int statusCode = client.executeMethod(method); + + assertEquals("HTTP GET failed", HttpStatus.SC_OK, statusCode); + + String response = method.getResponseBodyAsString(10000); + + assertTrue("Unexpected response body", response.contains("The population of NYC is 8550405")); + } finally { + method.releaseConnection(); + } + } +} diff --git a/boost-maven/boost-maven-plugin/src/it/test-dev-release/src/it/release-project/src/test/java/it/FeatureVersionIT.java b/boost-maven/boost-maven-plugin/src/it/test-dev-release/src/it/release-project/src/test/java/it/FeatureVersionIT.java new file mode 100644 index 00000000..107a528c --- /dev/null +++ b/boost-maven/boost-maven-plugin/src/it/test-dev-release/src/it/release-project/src/test/java/it/FeatureVersionIT.java @@ -0,0 +1,52 @@ +/******************************************************************************* + * Copyright (c) 2018 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 + *******************************************************************************/ +package it; + +import static org.junit.Assert.*; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; + +import org.junit.Test; + +public class FeatureVersionIT { + + private static final String JDBC_42_FEATURE = "jdbc-4.2"; + private static String SERVER_XML = "target/liberty/wlp/usr/servers/BoostServer/server.xml"; + + @Test + public void testFeatureVersion() throws Exception { + File targetFile = new File(SERVER_XML); + assertTrue(targetFile.getCanonicalFile() + "does not exist.", targetFile.exists()); + + // Check contents of file for jaxrs feature + boolean found = false; + BufferedReader br = null; + + try { + br = new BufferedReader(new FileReader(SERVER_XML)); + String line; + while ((line = br.readLine()) != null) { + if (line.contains(JDBC_42_FEATURE)) { + found = true; + break; + } + } + } finally { + if (br != null) { + br.close(); + } + } + + assertTrue("The " + JDBC_42_FEATURE + " feature was not found in the server configuration", found); + } +} diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/AbstractLibertyMojo.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/AbstractLibertyMojo.java index ac41df64..4630c94b 100644 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/AbstractLibertyMojo.java +++ b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/AbstractLibertyMojo.java @@ -18,6 +18,8 @@ import static org.twdata.maven.mojoexecutor.MojoExecutor.plugin; import static org.twdata.maven.mojoexecutor.MojoExecutor.version; +import java.util.List; + import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Plugin; import org.apache.maven.plugin.BuildPluginManager; @@ -27,6 +29,9 @@ import org.apache.maven.project.MavenProject; import org.codehaus.mojo.pluginsupport.MojoSupport; import org.codehaus.mojo.pluginsupport.util.ArtifactItem; +import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.repository.RemoteRepository; import org.twdata.maven.mojoexecutor.MojoExecutor.Element; import org.twdata.maven.mojoexecutor.MojoExecutor.ExecutionEnvironment; @@ -51,9 +56,18 @@ public abstract class AbstractLibertyMojo extends MojoSupport { @Parameter(defaultValue = "${session}", readonly = true) protected MavenSession session; + + @Parameter(defaultValue = "${repositorySystemSession}", readonly = true) + protected RepositorySystemSession repoSession; + + @Parameter( defaultValue = "${project.remoteProjectRepositories}", readonly = true, required = true ) + protected List remoteRepos; @Component protected BuildPluginManager pluginManager; + + @Component + protected RepositorySystem repoSystem; @Parameter protected ArtifactItem runtimeArtifact; @@ -95,8 +109,7 @@ private void createDefaultRuntimeArtifactIfNeeded() { } @Override - public void execute() throws MojoExecutionException { + public void execute() throws MojoExecutionException { createDefaultRuntimeArtifactIfNeeded(); } - } diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/LibertyPackageMojo.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/LibertyPackageMojo.java index ecf7f6b4..c9bf458b 100644 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/LibertyPackageMojo.java +++ b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/liberty/LibertyPackageMojo.java @@ -12,9 +12,11 @@ import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.List; import java.util.Map; +import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugins.annotations.Mojo; import org.apache.commons.io.FileUtils; @@ -77,6 +79,7 @@ public void execute() throws MojoExecutionException { // with a // spring // boot app + springBootClassifier = net.wasdev.wlp.maven.plugins.utils.SpringBootUtil .getSpringBootMavenPluginClassifier(project, getLog()); @@ -99,7 +102,7 @@ public void execute() throws MojoExecutionException { generateServerConfigSpringBoot(); installMissingFeatures(); - installApp(ConfigConstants.SPRING_BOOT_PROJ); + installApp(ConfigConstants.INSTALL_PACKAGE_SPRING); if (springBootUberJar != null) { // Create the Liberty Uber JAR from the Spring Boot Uber JAR in @@ -119,17 +122,17 @@ public void execute() throws MojoExecutionException { } } else { // Dealing with an EE based app - // Get booster dependencies from project - Map dependencies = MavenProjectUtil.getAllDependencies(project, BoostLogger.getInstance()); - - // Determine the Java compiler target version and set this - // internally + // Get the Java compiler target version String javaCompilerTargetVersion = MavenProjectUtil.getJavaCompilerTargetVersion(project); System.setProperty(BoostProperties.INTERNAL_COMPILER_TARGET, javaCompilerTargetVersion); try { + Map dependencies = MavenProjectUtil.getAllDependencies(project, repoSystem, repoSession, + remoteRepos, BoostLogger.getInstance()); + this.boosterPackConfigurators = BoosterConfigurator.getBoosterPackConfigurators(dependencies, BoostLogger.getInstance()); + } catch (Exception e) { throw new MojoExecutionException(e.getMessage(), e); } @@ -144,7 +147,15 @@ public void execute() throws MojoExecutionException { // If we installed the app prior to server.xml configuration, then // the LMP would write out a webapp stanza into config dropins that // would include a config-root setting set to the app name. - installApp(ConfigConstants.NORMAL_PROJ); + if (project.getPackaging().equals("war")) { + installApp(ConfigConstants.INSTALL_PACKAGE_ALL); + } else { + // This is temporary. When packing type is "jar", if we + // set installAppPackages=all, the LMP will try to install + // the project jar and fail. Once this is fixed, we can always + // set installAppPackages=all. + installApp(ConfigConstants.INSTALL_PACKAGE_DEP); + } // Not sure this works yet, the main idea is to NOT create this with // a WAR @@ -165,7 +176,8 @@ private void generateServerConfigSpringBoot() throws MojoExecutionException { try { // Get Spring Boot starters from Maven project - Map dependencies = MavenProjectUtil.getAllDependencies(project, BoostLogger.getInstance()); + Map dependencies = MavenProjectUtil.getAllDependencies(project, repoSystem, repoSession, + remoteRepos, BoostLogger.getInstance()); // Generate server config SpringBootUtil.generateLibertyServerConfig(projectBuildDir + "/classes", libertyServerPath, @@ -184,19 +196,10 @@ private void generateServerConfigSpringBoot() throws MojoExecutionException { */ private void generateServerConfigEE() throws MojoExecutionException { - String warName = null; - - if (project.getPackaging().equals(ConfigConstants.WAR_PKG_TYPE)) { - if (project.getVersion() == null) { - warName = project.getArtifactId(); - } else { - warName = project.getArtifactId() + "-" + project.getVersion(); - } - } - + List warNames = getWarNames(); try { // Generate server config - BoosterConfigurator.generateLibertyServerConfig(libertyServerPath, boosterPackConfigurators, warName, + BoosterConfigurator.generateLibertyServerConfig(libertyServerPath, boosterPackConfigurators, warNames, BoostLogger.getInstance()); } catch (Exception e) { @@ -204,6 +207,26 @@ private void generateServerConfigEE() throws MojoExecutionException { } } + private List getWarNames() { + List warNames = new ArrayList(); + + for (Artifact artifact : project.getArtifacts()) { + if (artifact.getType().equals("war")) { + warNames.add(artifact.getArtifactId() + "-" + artifact.getVersion()); + } + } + + if (project.getPackaging().equals(ConfigConstants.WAR_PKG_TYPE)) { + if (project.getVersion() == null) { + warNames.add(project.getArtifactId()); + } else { + warNames.add(project.getArtifactId() + "-" + project.getVersion()); + } + } + + return warNames; + } + /** * Manipulate the manifest so that Spring Boot will not attempt to repackage * a Liberty Uber JAR. @@ -293,7 +316,7 @@ private void installApp(String installAppPackagesVal) throws MojoExecutionExcept Element serverName = element(name("serverName"), libertyServerName); Xpp3Dom configuration = configuration(installAppPackages, serverName, getRuntimeArtifactElement()); - if (ConfigConstants.NORMAL_PROJ.equals(installAppPackagesVal)) { + if (!ConfigConstants.INSTALL_PACKAGE_SPRING.equals(installAppPackagesVal)) { configuration.addChild(element(name("appsDirectory"), "apps").toDom()); configuration.addChild(element(name("looseApplication"), "true").toDom()); } @@ -363,5 +386,4 @@ private void createUberJar(String packageFilePath, boolean attach) throws MojoEx element(name("packageFile"), packageFilePath), element(name("serverName"), libertyServerName)), getExecutionEnvironment()); } - } diff --git a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/utils/MavenProjectUtil.java b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/utils/MavenProjectUtil.java index f0b5d0df..bd31e104 100644 --- a/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/utils/MavenProjectUtil.java +++ b/boost-maven/boost-maven-plugin/src/main/java/io/openliberty/boost/maven/utils/MavenProjectUtil.java @@ -10,13 +10,25 @@ *******************************************************************************/ package io.openliberty.boost.maven.utils; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; import org.apache.maven.artifact.Artifact; import org.apache.maven.project.MavenProject; +import org.eclipse.aether.RepositorySystem; +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.artifact.DefaultArtifact; +import org.eclipse.aether.collection.CollectRequest; +import org.eclipse.aether.graph.Dependency; +import org.eclipse.aether.repository.RemoteRepository; +import org.eclipse.aether.resolution.ArtifactResult; +import org.eclipse.aether.resolution.DependencyRequest; +import org.eclipse.aether.resolution.DependencyResolutionException; +import org.eclipse.aether.resolution.DependencyResult; public class MavenProjectUtil { @@ -37,16 +49,43 @@ public static String findSpringBootVersion(MavenProject project) { return version; } - public static Map getAllDependencies(MavenProject project, BoostLogger logger) { - - Map dependencies = new HashMap(); + public static Map getAllDependencies(MavenProject project, RepositorySystem repoSystem, + RepositorySystemSession repoSession, List remoteRepos, BoostLogger logger) + throws DependencyResolutionException { logger.debug("Processing project for dependencies."); + Map dependencies = new HashMap(); for (Artifact artifact : project.getArtifacts()) { logger.debug("Found dependency while processing project: " + artifact.getGroupId() + ":" - + artifact.getArtifactId() + ":" + artifact.getVersion()); - - dependencies.put(artifact.getGroupId() + ":" + artifact.getArtifactId(), artifact.getVersion()); + + artifact.getArtifactId() + ":" + artifact.getVersion() + ":" + artifact.getType()); + + if (artifact.getType().equals("war")) { + logger.debug("Resolving transitive booster dependencies for war"); + org.eclipse.aether.artifact.Artifact pomArtifact = new DefaultArtifact(artifact.getGroupId(), + artifact.getArtifactId(), "pom", artifact.getVersion()); + + List warArtifacts = resolveArtifact(pomArtifact, repoSystem, + repoSession, remoteRepos); + + for (org.eclipse.aether.artifact.Artifact warArtifact : warArtifacts) { + // Only add booster dependencies for this war. Anything else + // like datasource + // dependencies must be explicitly defined by this project. + // This is to allow the + // current project to have full control over those optional + // dependencies. + if (warArtifact.getGroupId().equals("io.openliberty.boosters")) { + BoostLogger.getInstance().debug("Found booster dependency: " + warArtifact.getGroupId() + ":" + + warArtifact.getArtifactId() + ":" + warArtifact.getVersion()); + + dependencies.put(warArtifact.getGroupId() + ":" + warArtifact.getArtifactId(), + warArtifact.getVersion()); + } + } + + } else { + dependencies.put(artifact.getGroupId() + ":" + artifact.getArtifactId(), artifact.getVersion()); + } } return dependencies; @@ -85,4 +124,26 @@ public static String getJavaCompilerTargetVersion(MavenProject project) { return ""; } + private static List resolveArtifact( + org.eclipse.aether.artifact.Artifact artifact, RepositorySystem repoSystem, + RepositorySystemSession repoSession, List remoteRepos) + throws DependencyResolutionException { + CollectRequest collectRequest = new CollectRequest(); + collectRequest.setRoot(new Dependency(artifact, "import")); + for (RemoteRepository remoteRepo : remoteRepos) { + collectRequest.addRepository(remoteRepo); + } + + DependencyResult resolutionResult = repoSystem.resolveDependencies(repoSession, + new DependencyRequest(collectRequest, null)); + + List artifactResults = resolutionResult.getArtifactResults(); + ArrayList artifacts = new ArrayList<>(artifactResults.size()); + + for (ArtifactResult result : artifactResults) { + artifacts.add(result.getArtifact()); + } + + return artifacts; + } }