Skip to content

Commit

Permalink
FORGE-2584: Fixed profile usage with ProjectBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Feb 17, 2017
1 parent aaf7086 commit b2836eb
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class MavenProjectBuilder implements ProjectBuilder

private boolean runTests = true;
private boolean quiet;
private final List<String> profiles = new ArrayList<>();
private final List<String> args = new ArrayList<>();

public MavenProjectBuilder(final Environment environment, final Project project)
Expand Down Expand Up @@ -59,6 +60,14 @@ public ProjectBuilder quiet(boolean quiet)
return this;
}

@Override
public ProjectBuilder profiles(String... profiles)
{
this.profiles.clear();
this.profiles.addAll(Arrays.asList(profiles));
return this;
}

@Override
public Resource<?> build()
{
Expand Down Expand Up @@ -94,6 +103,10 @@ public Resource<?> build(PrintStream out, PrintStream err) throws BuildException
{
selected.add("-q");
}
if (profiles.size() > 0)
{
selected.add("-P" + String.join(",", profiles));
}
boolean success = project.getFacet(MavenFacet.class).executeMavenEmbedded(selected, out, err);

if (success)
Expand Down
9 changes: 5 additions & 4 deletions maven/tests/pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<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/xsd/maven-4.0.0.xsd">
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jboss.forge.addon</groupId>
Expand Down Expand Up @@ -64,9 +65,9 @@

<!-- Testing -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>1.7.1</version>
<groupId>org.jboss.forge.addon</groupId>
<artifactId>assertj</artifactId>
<classifier>forge-addon</classifier>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2017 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.addon.maven.projects;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import org.jboss.arquillian.junit.Arquillian;
import org.jboss.forge.addon.projects.Project;
import org.jboss.forge.addon.projects.ProjectFactory;
import org.jboss.forge.addon.projects.building.ProjectBuilder;
import org.jboss.forge.addon.projects.facets.PackagingFacet;
import org.jboss.forge.furnace.container.simple.lifecycle.SimpleContainer;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
*
* @author <a href="mailto:ggastald@redhat.com">George Gastaldi</a>
*/
@RunWith(Arquillian.class)
public class MavenProjectBuilderTest
{

private ProjectFactory projectFactory;

@Before
public void setUp()
{
projectFactory = SimpleContainer.getServices(getClass().getClassLoader(), ProjectFactory.class).get();
}

/**
* Test method for {@link org.jboss.forge.addon.maven.projects.MavenProjectBuilder#build()}.
*/
@Test
public void testBuild() throws Exception
{
Project project = projectFactory.createTempProject();
ProjectBuilder projectBuilder = project.getFacet(PackagingFacet.class).createBuilder();
projectBuilder.profiles("foo", "bar");
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream err = new ByteArrayOutputStream();
projectBuilder.build(new PrintStream(out, true), new PrintStream(err, true));
assertThat(out.toString()).contains("BUILD SUCCESS",
"[WARNING] The requested profile \"foo\" could not be activated because it does not exist.",
"[WARNING] The requested profile \"bar\" could not be activated because it does not exist.");
assertThat(err.toString()).isEmpty();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ public interface ProjectBuilder
*/
ProjectBuilder runTests(boolean test);

/**
* Profiles to be enabled for this build (ignored if not applicable to the project build system)
*/
default ProjectBuilder profiles(String... profiles)
{
// Default implementation is a NOOP
return this;
}

/**
* Execute the build, returning the final product as a {@link Resource}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.jboss.forge.addon.ui.util.Categories;
import org.jboss.forge.addon.ui.util.Metadata;
import org.jboss.forge.furnace.container.simple.lifecycle.SimpleContainer;
import org.jboss.forge.furnace.util.Lists;

/**
* Executes Build commands
Expand All @@ -39,7 +40,7 @@ public class BuildCommand extends AbstractProjectCommand
private UIInputMany<String> arguments;
private UIInput<Boolean> notest;
private UIInput<Boolean> quiet;
private UIInput<String> profile;
private UIInputMany<String> profile;

@Override
public void initializeUI(UIBuilder builder) throws Exception
Expand All @@ -48,7 +49,7 @@ public void initializeUI(UIBuilder builder) throws Exception
arguments = factory.createInputMany("arguments", String.class);
notest = factory.createInput("notest", Boolean.class).setLabel("No Test");
quiet = factory.createInput("quiet", 'q', Boolean.class).setLabel("Quiet").setDescription("Quiet output");
profile = factory.createInput("profile", String.class);
profile = factory.createInputMany("profile", String.class);
builder.add(arguments).add(profile).add(notest).add(quiet);
}

Expand All @@ -69,7 +70,7 @@ public Result execute(UIExecutionContext context) throws Exception

if (arguments.getValue() != null && arguments.getValue().iterator().hasNext())
{
List<String> args = new ArrayList<String>();
List<String> args = new ArrayList<>();
for (String val : arguments.getValue())
{
args.add(val);
Expand All @@ -84,7 +85,8 @@ public Result execute(UIExecutionContext context) throws Exception

if (profile.hasValue())
{
builder.addArguments("-P" + profile.getValue());
List<String> list = Lists.toList(profile.getValue());
builder.profiles(list.toArray(new String[list.size()]));
}

builder.quiet(quiet.getValue());
Expand Down

0 comments on commit b2836eb

Please sign in to comment.