[MNG-6294] Convert MavenPluginValidator into a JSR 330 component

Modified by: Guillaume Dufour <guillaume.duff@gmail.com>

This closes #134 and closes #470
This commit is contained in:
Michael Simacek 2017-10-12 14:55:46 +03:00 committed by Michael Osipov
parent 9ba84aabe2
commit 83e36649c0
4 changed files with 157 additions and 41 deletions

View File

@ -31,7 +31,6 @@ import org.apache.maven.plugin.DebugConfigurationListener;
import org.apache.maven.plugin.ExtensionRealmCache;
import org.apache.maven.plugin.InvalidPluginDescriptorException;
import org.apache.maven.plugin.MavenPluginManager;
import org.apache.maven.plugin.MavenPluginValidator;
import org.apache.maven.plugin.Mojo;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoNotFoundException;
@ -166,6 +165,9 @@ public class DefaultMavenPluginManager
@Inject
private PluginArtifactsCache pluginArtifactsCache;
@Inject
private MavenPluginValidator pluginValidator;
private ExtensionDescriptorBuilder extensionDescriptorBuilder = new ExtensionDescriptorBuilder();
private PluginDescriptorBuilder builder = new PluginDescriptorBuilder();
@ -243,14 +245,13 @@ public class DefaultMavenPluginManager
throw new PluginDescriptorParsingException( plugin, pluginFile.getAbsolutePath(), e );
}
MavenPluginValidator validator = new MavenPluginValidator( pluginArtifact );
List<String> errors = new ArrayList<>();
pluginValidator.validate( pluginArtifact, pluginDescriptor, errors );
validator.validate( pluginDescriptor );
if ( validator.hasErrors() )
if ( !errors.isEmpty() )
{
throw new InvalidPluginDescriptorException(
"Invalid plugin descriptor for " + plugin.getId() + " (" + pluginFile + ")", validator.getErrors() );
"Invalid plugin descriptor for " + plugin.getId() + " (" + pluginFile + ")", errors );
}
pluginDescriptor.setPluginArtifact( pluginArtifact );

View File

@ -1,4 +1,4 @@
package org.apache.maven.plugin;
package org.apache.maven.plugin.internal;
/*
* Licensed to the Apache Software Foundation (ASF) under one
@ -19,40 +19,26 @@ package org.apache.maven.plugin;
* under the License.
*/
import java.util.ArrayList;
import java.util.List;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import javax.inject.Named;
import javax.inject.Singleton;
/**
* MavenPluginValidator
* DefaultMavenPluginValidator
*/
public class MavenPluginValidator
@Named
@Singleton
class DefaultMavenPluginValidator
implements MavenPluginValidator
{
private final Artifact pluginArtifact;
private List<String> errors = new ArrayList<>();
private boolean firstDescriptor = true;
public MavenPluginValidator( Artifact pluginArtifact )
@Override
public void validate( Artifact pluginArtifact, PluginDescriptor pluginDescriptor, List<String> errors )
{
this.pluginArtifact = pluginArtifact;
}
public void validate( PluginDescriptor pluginDescriptor )
{
/*
* NOTE: For plugins that depend on other plugin artifacts the plugin realm contains more than one plugin
* descriptor. However, only the first descriptor is of interest.
*/
if ( !firstDescriptor )
{
return;
}
firstDescriptor = false;
if ( !pluginArtifact.getGroupId().equals( pluginDescriptor.getGroupId() ) )
{
errors.add( "Plugin's descriptor contains the wrong group ID: " + pluginDescriptor.getGroupId() );
@ -68,14 +54,4 @@ public class MavenPluginValidator
errors.add( "Plugin's descriptor contains the wrong version: " + pluginDescriptor.getVersion() );
}
}
public boolean hasErrors()
{
return !errors.isEmpty();
}
public List<String> getErrors()
{
return errors;
}
}

View File

@ -0,0 +1,34 @@
package org.apache.maven.plugin.internal;
/*
* 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.util.List;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
/**
* MavenPluginValidator
*/
interface MavenPluginValidator
{
void validate( Artifact pluginArtfiact, PluginDescriptor pluginDescriptor, List<String> errors );
}

View File

@ -0,0 +1,105 @@
package org.apache.maven.plugin.internal;
/*
* 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.util.ArrayList;
import java.util.List;
import org.apache.maven.AbstractCoreMavenComponentTestCase;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.plugin.descriptor.PluginDescriptor;
import org.apache.maven.plugin.internal.MavenPluginValidator;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import javax.inject.Inject;
/**
* @author Michael Simacek
*/
public class MavenPluginValidatorTest extends AbstractCoreMavenComponentTestCase
{
@Inject
private MavenPluginValidator mavenPluginValidator;
protected String getProjectsDirectory()
{
return "src/test/projects/default-maven";
}
@Test
public void testValidate()
{
Artifact plugin = new DefaultArtifact( "org.apache.maven.its.plugins", "maven-it-plugin", "0.1", "compile",
"jar", null, new DefaultArtifactHandler() );
PluginDescriptor descriptor = new PluginDescriptor();
descriptor.setGroupId( "org.apache.maven.its.plugins" );
descriptor.setArtifactId( "maven-it-plugin" );
descriptor.setVersion( "0.1" );
List<String> errors = new ArrayList<>();
mavenPluginValidator.validate( plugin, descriptor, errors );
assertTrue( errors.isEmpty() );
}
@Test
public void testInvalidGroupId()
{
Artifact plugin = new DefaultArtifact( "org.apache.maven.its.plugins", "maven-it-plugin", "0.1", "compile",
"jar", null, new DefaultArtifactHandler() );
PluginDescriptor descriptor = new PluginDescriptor();
descriptor.setGroupId( "org.apache.maven.its.plugins.invalid" );
descriptor.setArtifactId( "maven-it-plugin" );
descriptor.setVersion( "0.1" );
List<String> errors = new ArrayList<>();
mavenPluginValidator.validate( plugin, descriptor, errors );
assertFalse( errors.isEmpty() );
}
@Test
public void testInvalidArtifactId()
{
Artifact plugin = new DefaultArtifact( "org.apache.maven.its.plugins", "maven-it-plugin", "0.1", "compile",
"jar", null, new DefaultArtifactHandler() );
PluginDescriptor descriptor = new PluginDescriptor();
descriptor.setGroupId( "org.apache.maven.its.plugins" );
descriptor.setArtifactId( "maven-it-plugin.invalid" );
descriptor.setVersion( "0.1" );
List<String> errors = new ArrayList<>();
mavenPluginValidator.validate( plugin, descriptor, errors );
assertFalse( errors.isEmpty() );
}
@Test
public void testInvalidVersion()
{
Artifact plugin = new DefaultArtifact( "org.apache.maven.its.plugins", "maven-it-plugin", "0.1", "compile",
"jar", null, new DefaultArtifactHandler() );
PluginDescriptor descriptor = new PluginDescriptor();
descriptor.setGroupId( "org.apache.maven.its.plugins" );
descriptor.setArtifactId( "maven-it-plugin" );
List<String> errors = new ArrayList<>();
mavenPluginValidator.validate( plugin, descriptor, errors );
assertFalse( errors.isEmpty() );
}
}