diff --git a/its/core-it-suite/src/test/java/org/apache/maven/it/IntegrationTestSuite.java b/its/core-it-suite/src/test/java/org/apache/maven/it/IntegrationTestSuite.java index 39b4d9d1c8..c108811451 100644 --- a/its/core-it-suite/src/test/java/org/apache/maven/it/IntegrationTestSuite.java +++ b/its/core-it-suite/src/test/java/org/apache/maven/it/IntegrationTestSuite.java @@ -98,6 +98,7 @@ public class IntegrationTestSuite suite.addTestSuite( MavenITmng4629NoPomValidationErrorUponMissingSystemDepTest.class ); suite.addTestSuite( MavenITmng4625SettingsXmlInterpolationWithXmlMarkupTest.class ); suite.addTestSuite( MavenITmng4618AggregatorBuiltAfterModulesTest.class ); + suite.addTestSuite( MavenITmng4615ValidateRequiredPluginParameterTest.class ); suite.addTestSuite( MavenITmng4600DependencyOptionalFlagManagementTest.class ); suite.addTestSuite( MavenITmng4590ImportedPomUsesSystemPropertiesTest.class ); suite.addTestSuite( MavenITmng4586PluginPrefixResolutionFromVersionlessPluginMngtTest.class ); diff --git a/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng4615ValidateRequiredPluginParameterTest.java b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng4615ValidateRequiredPluginParameterTest.java new file mode 100644 index 0000000000..e264ef9665 --- /dev/null +++ b/its/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng4615ValidateRequiredPluginParameterTest.java @@ -0,0 +1,86 @@ +package org.apache.maven.it; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import org.apache.maven.it.Verifier; +import org.apache.maven.it.util.ResourceExtractor; + +import java.io.File; +import java.util.Properties; + +/** + * This is a test set for MNG-4615. + * + * @author Benjamin Bentmann + */ +public class MavenITmng4615ValidateRequiredPluginParameterTest + extends AbstractMavenIntegrationTestCase +{ + + public MavenITmng4615ValidateRequiredPluginParameterTest() + { + super( "[2.0.3,3.0-alpha-1),[3.0-beta-2,)" ); + } + + /** + * Verify that Maven validates required mojo parameters (and doesn't just have the plugins die with NPEs). + */ + public void testit() + throws Exception + { + File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-4615" ); + + Verifier verifier = newVerifier( testDir.getAbsolutePath() ); + verifier.setAutoclean( false ); + verifier.deleteDirectory( "target" ); + try + { + verifier.executeGoal( "validate" ); + verifier.verifyErrorFreeLog(); + fail( "Build did not fail despite required plugin parameter missing" ); + } + catch ( VerificationException e ) + { + // expected + } + + // sanity check that adding the param makes it work + + verifier.setLogFileName( "log-2.txt" ); + verifier.getCliOptions().add( "-Pmng4615" ); + verifier.deleteDirectory( "target" ); + verifier.executeGoal( "validate" ); + verifier.verifyErrorFreeLog(); + Properties props = verifier.loadProperties( "target/config.properties" ); + assertEquals( "PROFILE", props.get( "requiredParam" ) ); + + verifier.setLogFileName( "log-3.txt" ); + verifier.getCliOptions().remove( "-Pmng4615" ); + verifier.setSystemProperty( "config.requiredParam", "CLI" ); + verifier.deleteDirectory( "target" ); + verifier.executeGoal( "validate" ); + verifier.verifyErrorFreeLog(); + props = verifier.loadProperties( "target/config.properties" ); + assertEquals( "CLI", props.get( "requiredParam" ) ); + + verifier.resetStreams(); + } + +} diff --git a/its/core-it-suite/src/test/resources/mng-4615/pom.xml b/its/core-it-suite/src/test/resources/mng-4615/pom.xml new file mode 100644 index 0000000000..d551708182 --- /dev/null +++ b/its/core-it-suite/src/test/resources/mng-4615/pom.xml @@ -0,0 +1,73 @@ + + + + + + 4.0.0 + + org.apache.maven.its.mng4615 + test + 0.1 + jar + + Maven Integration Test :: MNG-4615 + + Verify that Maven validates required mojo parameters (and doesn't just have the plugins die with NPEs). + + + + + + org.apache.maven.its.plugins + maven-it-plugin-configuration + 2.1-SNAPSHOT + + target/config.properties + + + + test + validate + + required-config + + + + + + + + + + mng4615 + + + + org.apache.maven.its.plugins + maven-it-plugin-configuration + + PROFILE + + + + + + + diff --git a/its/core-it-support/core-it-plugins/maven-it-plugin-configuration/src/main/java/org/apache/maven/plugin/coreit/RequiredConfigMojo.java b/its/core-it-support/core-it-plugins/maven-it-plugin-configuration/src/main/java/org/apache/maven/plugin/coreit/RequiredConfigMojo.java new file mode 100644 index 0000000000..ac0736d600 --- /dev/null +++ b/its/core-it-support/core-it-plugins/maven-it-plugin-configuration/src/main/java/org/apache/maven/plugin/coreit/RequiredConfigMojo.java @@ -0,0 +1,113 @@ +package org.apache.maven.plugin.coreit; + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import java.io.File; +import java.util.Properties; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; + +/** + * Dumps this mojo's configuration into a properties file. + * + * @goal required-config + * @phase validate + * + * @author Benjamin Bentmann + */ +public class RequiredConfigMojo + extends AbstractMojo +{ + + /** + * The current project's base directory, used for path alignment. + * + * @parameter default-value="${basedir}" + * @readonly + */ + private File basedir; + + /** + * The path to the properties file into which to save the mojo configuration. + * + * @parameter expression="${config.propertiesFile}" + */ + private File propertiesFile; + + /** + * A required parameter. + * + * @parameter expression="${config.requiredParam}" + * @required + */ + private String requiredParam; + + /** + * A required parameter that is actually backed by default value from the POM. + * + * @parameter default-value="${project.artifactId}" + * @required + */ + private String requiredParamWithDefault; + + /** + * Runs this mojo. + * + * @throws MojoExecutionException If the output file could not be created. + */ + public void execute() + throws MojoExecutionException + { + getLog().info( "[MAVEN-CORE-IT-LOG] Using output file path: " + propertiesFile ); + + if ( propertiesFile == null ) + { + throw new MojoExecutionException( "Path name for output file has not been specified" ); + } + + if ( !propertiesFile.isAbsolute() ) + { + propertiesFile = new File( basedir, propertiesFile.getPath() ).getAbsoluteFile(); + } + + Properties configProps = new Properties(); + + dumpConfiguration( configProps ); + + getLog().info( "[MAVEN-CORE-IT-LOG] Creating output file: " + propertiesFile ); + + PropertiesUtil.write( propertiesFile, configProps ); + + getLog().info( "[MAVEN-CORE-IT-LOG] Created output file: " + propertiesFile ); + } + + /** + * Dumps the mojo configuration into the specified properties. + * + * @param props The properties to dump the configuration into, must not be null. + */ + private void dumpConfiguration( Properties props ) + { + PropertiesUtil.serialize( props, "requiredParam", requiredParam ); + PropertiesUtil.serialize( props, "requiredParamWithDefault", requiredParamWithDefault ); + } + +}