Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the ability of failing the goal if a key has no value #2

Open
wants to merge 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}
}

final void saveSingleFile(final SingleFile aFile) {
final void saveSingleFile(final SingleFile aFile) throws MojoFailureException {
TFile input = new TFile(aFile.getSourceFile());
TFile output = new TFile(aFile.getTargetFileName());
try {
Expand Down Expand Up @@ -117,7 +117,7 @@ final String filterMustacheOnly(final String aContent) {
return builder.toString();
}

protected abstract String transform(final String content);
protected abstract String transform(final String content) throws MojoFailureException;

public List<FileSet> getFilesets() {
return filesets;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.xebialabs.maven.mustache;

import com.xebialabs.maven.mustache.transformer.MustacheToKey;
import org.apache.maven.plugin.MojoFailureException;


/**
Expand All @@ -13,7 +14,7 @@
*/
public class MustachifierToKeyMojo extends AbstractMustachifierMojo {

protected String transform(final String content) {
protected String transform(final String content) throws MojoFailureException {
return new MustacheToKey(getBeforeDelimiter(), getAfterDelimiter(), getValueSeparator()).process(content);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.xebialabs.maven.mustache;

import com.xebialabs.maven.mustache.transformer.MustacheToValue;
import org.apache.maven.plugin.MojoFailureException;


/**
Expand All @@ -15,15 +16,21 @@ public class MustachifierToValueMojo extends AbstractMustachifierMojo {


/**
* The separator format from the placeholder name and the default value
* The input for values (property file or XLDeploy instance)
*
* @parameter
*/
protected ValueProviderConfiguration valueProvider = new ValueProviderConfiguration();

/**
* True if the goal should rise an error if not finding a value, false by default otherwise
*
* @parameter
*/
protected Boolean mandatoryValues = false;

protected String transform(final String content) {
return new MustacheToValue(getBeforeDelimiter(), getAfterDelimiter(), getValueSeparator(), valueProvider.getValueProvider()).process(content);
protected String transform(final String content) throws MojoFailureException {
return new MustacheToValue(getBeforeDelimiter(), getAfterDelimiter(), getValueSeparator(), valueProvider.getValueProvider(), mandatoryValues).process(content);
}

public ValueProviderConfiguration getValueProvider() {
Expand All @@ -33,4 +40,12 @@ public ValueProviderConfiguration getValueProvider() {
public void setValueProvider(final ValueProviderConfiguration valueProvider) {
this.valueProvider = valueProvider;
}

public void setMandatoryValues(final Boolean mandatoryValues) {
this.mandatoryValues = mandatoryValues;
}

public Boolean isMandatoryValues() {
return mandatoryValues;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.samskivert.mustache.DefaultCollector;
import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.MustacheException;
import org.apache.maven.plugin.MojoFailureException;

public abstract class DefaultMustacheCollector extends DefaultCollector implements Mustache.VariableFetcher {

Expand All @@ -28,7 +30,12 @@ private Mustache.Compiler getCompiler() {
.standardsMode(true);
}

public String process(String content) {
return getCompiler().compile(content).execute(new Object());
public String process(String content) throws MojoFailureException {
try {
return getCompiler().compile(content).execute(new Object());
} catch (MustacheException e) {
throw new MojoFailureException(e.getMessage());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@

import com.xebialabs.maven.mustache.provider.ValueProvider;

import org.apache.maven.plugin.MojoFailureException;

public class MustacheToValue extends DefaultMustacheCollector implements Mustache.VariableFetcher {

final ValueProvider valueProvider;

public MustacheToValue(final String beforeDelimiter, final String afterDelimiter, final String valueSeparator, final ValueProvider valueProvider) {
final Boolean mandatoryValues;

public MustacheToValue(final String beforeDelimiter, final String afterDelimiter, final String valueSeparator,
final ValueProvider valueProvider, final Boolean mandatoryValues) {
super(afterDelimiter, beforeDelimiter, valueSeparator);
this.valueProvider = valueProvider;

this.mandatoryValues = mandatoryValues;
}

@Override
Expand All @@ -20,6 +27,9 @@ public Object get(final Object ctx, final String name) throws Exception {
return valueProvider.getValue(name.split(valueSeparator)[0], index == name.length() - 1 ? "" : name.substring(index + 1));
}
else
if (mandatoryValues) {
return null;
}
return valueProvider.getValue(name, beforeDelimiter + name + afterDelimiter);
}

Expand Down
2 changes: 1 addition & 1 deletion src/site/markdown/keys.md.vm
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ the property file **before** the plugin execution

the property file **after** the plugin execution

param1=45
param1={{PARAM1}}
param2={{param22}}
param3=44
param4=http://{{host}}:{{port}}/{{context}}
Expand Down
1 change: 1 addition & 0 deletions src/site/markdown/values-with-properties.md.vm
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ the property file **after** the plugin execution
param4=http://www.xebialabs.com:9090/xldeploy
param5=benoit

Setting the parameter <mandatoryValues>true</mandatoryValues> ensures that all keys must have a value in the valueProvider, if not, the Maven goal fails.
3 changes: 2 additions & 1 deletion src/site/markdown/values-with-xldeploy.md.vm
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Why using the effective dictionary and not a dictionary ? The effective dictiona
<context>deployit</context>
<environment>Environments/Dev/Tomcat-Dev</environment>
</xldeploy>
</valueProvider>
</valueProvider>
</configuration>
<executions>
<execution>
Expand Down Expand Up @@ -58,3 +58,4 @@ the property file **after** the plugin execution
param4=http://www.xebialabs.com:9090/xldeploy
param5=benoit

Setting the parameter <mandatoryValues>true</mandatoryValues> ensures that all keys must have a value in the valueProvider, if not, the Maven goal fails.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.xebialabs.maven.mustache;

import java.io.File;
import java.util.Collections;
import java.util.Properties;

import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.shared.model.fileset.FileSet;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

import com.xebialabs.restito.server.StubServer;
import com.xebialabs.restito.support.junit.NeedsServer;
import com.xebialabs.restito.support.junit.ServerDependencyRule;

@RunWith(BlockJUnit4ClassRunner.class)
public class MustachifierToValueWithXLDeployValueProviderMandatoryMojoTest extends MustachifierMojoTest {

@Rule
public ServerDependencyRule serverDependencyRule = new ServerDependencyRule();

protected StubServer stubServer;

@Before
public void setUp() throws Exception {
super.setUp();
if (serverDependencyRule.isServerDependent()) {
stubServer = new StubServer(new XLDeployStandardBehavior());
stubServer.run();
}
}

@After
public void tearDown() throws Exception {
if (stubServer != null) {
stubServer.stop();
}
super.tearDown();
}

@Test(expected = MojoFailureException.class)
@NeedsServer
public void testSimpleConfigurationWithPlaceholdersInValues() throws Exception {
final File targetFile = new File("./target/test-classes/mustache/withplaceholders/config2.properties");
final FileSet fs = new FileSet();
fs.setDirectory(targetFile.getParent());
fs.setIncludes(Collections.singletonList(targetFile.getName()));

MustachifierToValueMojo mojo = getMojo();
mojo.setFilesets(Collections.singletonList(fs));
mojo.setValueSeparator(":");
mojo.getValueProvider().setFile(null);
mojo.execute();
}

private MustachifierToValueMojo getMojo() throws Exception {
return (MustachifierToValueMojo) getMojo("target/test-classes/mustache/pom-tovalue-with-value-provider-mandatory.xml", "mustache-tovalue");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<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>

<groupId>org.apache.maven.plugin.my.unit</groupId>
<artifactId>project-to-test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Test Mustache</name>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.0.4</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.xebialabs.utils</groupId>
<artifactId>placeholders-mustachifier-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<configuration>
<delimiters>{{ }}</delimiters>
<valueProvider>
<file>
<path>XXXX</path>
</file>
<xldeploy>
<username>admin</username>
<password>admin</password>
<url>http://localhost:6666</url>
<context>deployit</context>
<environment>Environments/Dev/Tomcat-Dev</environment>
</xldeploy>
</valueProvider>
<filesets>
<fileset>
<include>XXXXX</include>
</fileset>
</filesets>
<mandatoryValues>true</mandatoryValues>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>mustache-tovalue</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>
</build>
</project>