Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot test maven plugin (ClassNotFound - ArtifactTransformationManager)

when I try to test my maven plugin I get the following error:

WARNING: Error injecting: org.apache.maven.repository.legacy.LegacyRepositorySystem
com.google.inject.ProvisionException: Guice provision errors:

1) Error injecting: org.apache.maven.artifact.resolver.DefaultArtifactResolver
   at ClassRealm[plexus.core, parent: null]
   at ClassRealm[plexus.core, parent: null]
   while locating org.apache.maven.artifact.resolver.ArtifactResolver
   while locating org.apache.maven.repository.legacy.LegacyRepositorySystem

1 error
at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:977)
at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1003)
at org.sonatype.guice.bean.reflect.AbstractDeferredClass.get(AbstractDeferredClass.java:47)
at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:40)
at com.google.inject.internal.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:46)
at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1021)
at com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40)
at com.google.inject.Scopes$1$1.get(Scopes.java:59)
at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:40)
at com.google.inject.internal.InjectorImpl$4$1.call(InjectorImpl.java:968)
at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1014)
at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:964)
at org.sonatype.guice.bean.locators.LazyBeanEntry.getValue(LazyBeanEntry.java:79)
at org.sonatype.guice.plexus.locators.LazyPlexusBean.getValue(LazyPlexusBean.java:53)
at org.codehaus.plexus.DefaultPlexusContainer.lookup(DefaultPlexusContainer.java:243)
at org.codehaus.plexus.DefaultPlexusContainer.lookup(DefaultPlexusContainer.java:235)
at org.codehaus.plexus.DefaultPlexusContainer.lookup(DefaultPlexusContainer.java:229)
at org.codehaus.plexus.PlexusTestCase.lookup(PlexusTestCase.java:206)
at org.apache.maven.plugin.testing.AbstractMojoTestCase.setUp(AbstractMojoTestCase.java:118)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.junit.JUnitTestSet.execute(JUnitTestSet.java:213)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:338)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:997)
Caused by: java.lang.NoClassDefFoundError: Lorg/apache/maven/artifact/transform/ArtifactTransformationManager;
at java.lang.Class.getDeclaredFields0(Native Method)
at java.lang.Class.privateGetDeclaredFields(Class.java:2300)
at java.lang.Class.getDeclaredFields(Class.java:1745)
at com.google.inject.spi.InjectionPoint.getInjectionPoints(InjectionPoint.java:649)
at com.google.inject.spi.InjectionPoint.forInstanceMethodsAndFields(InjectionPoint.java:356)
at com.google.inject.internal.ConstructorBindingImpl.getInternalDependencies(ConstructorBindingImpl.java:151)
at com.google.inject.internal.InjectorImpl.getInternalDependencies(InjectorImpl.java:584)
at com.google.inject.internal.InjectorImpl.cleanup(InjectorImpl.java:542)
at com.google.inject.internal.InjectorImpl.initializeJitBinding(InjectorImpl.java:528)
at com.google.inject.internal.InjectorImpl.createJustInTimeBinding(InjectorImpl.java:837)
at com.google.inject.internal.InjectorImpl.createJustInTimeBindingRecursive(InjectorImpl.java:769)
at com.google.inject.internal.InjectorImpl.getJustInTimeBinding(InjectorImpl.java:254)
at com.google.inject.internal.InjectorImpl.getBindingOrThrow(InjectorImpl.java:205)
at com.google.inject.internal.InjectorImpl.getInternalFactory(InjectorImpl.java:843)
at com.google.inject.internal.InjectorImpl.getProviderOrThrow(InjectorImpl.java:957)
at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:990)
at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:951)
at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1003)
at org.sonatype.guice.bean.reflect.AbstractDeferredClass.get(AbstractDeferredClass.java:47)
at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:40)
at com.google.inject.internal.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:46)
at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1021)
at com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40)
at com.google.inject.Scopes$1$1.get(Scopes.java:59)
at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:40)
at com.google.inject.internal.InjectorImpl$4$1.call(InjectorImpl.java:968)
at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1021)
at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:964)
at org.sonatype.guice.bean.locators.LazyBeanEntry.getValue(LazyBeanEntry.java:79)
at org.sonatype.guice.plexus.locators.LazyPlexusBean.getValue(LazyPlexusBean.java:53)
at org.sonatype.guice.plexus.binders.PlexusRequirements$RequirementProvider.get(PlexusRequirements.java:221)
at org.sonatype.guice.plexus.binders.ProvidedPropertyBinding.injectProperty(ProvidedPropertyBinding.java:49)
at org.sonatype.guice.bean.inject.BeanInjector.doInjection(BeanInjector.java:105)
at org.sonatype.guice.bean.inject.BeanInjector.injectMembers(BeanInjector.java:66)
at com.google.inject.internal.MembersInjectorImpl.injectMembers(MembersInjectorImpl.java:120)
at com.google.inject.internal.ConstructorInjector.construct(ConstructorInjector.java:94)
at com.google.inject.internal.ConstructorBindingImpl$Factory.get(ConstructorBindingImpl.java:254)
at com.google.inject.internal.InjectorImpl$4$1.call(InjectorImpl.java:968)
at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1021)
at com.google.inject.internal.InjectorImpl$4.get(InjectorImpl.java:964)
... 39 more
Caused by: java.lang.ClassNotFoundException: org.apache.maven.artifact.transform.ArtifactTransformationManager
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 79 more

The pom of the project:

<dependencies>

    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-plugin-api</artifactId>
        <version>3.0.3</version>
    </dependency>

    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <version>2.9</version>
    </dependency>

    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-core</artifactId>
        <version>3.0.3</version>
    </dependency>

    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-compat</artifactId>
        <version>3.0.3</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-exec</artifactId>
        <version>1.1</version>
    </dependency>

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>

    <dependency>
        <groupId>org.apache.maven.plugin-testing</groupId>
        <artifactId>maven-plugin-testing-harness</artifactId>
        <version>2.1</version>
        <scope>test</scope>
    </dependency>

</dependencies>


<build>
    <plugins>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <additionalConfig>
                    <file>
                        <name>.checkstyle</name>
                        <location>../config/eclipse-checkstyle-config.xml</location>
                    </file>
                </additionalConfig>
            </configuration>
        </plugin>

    </plugins>

</build>

I tried to find the artifact with the missing class (org.apache.maven.artifact.transform.ArtifactTransformationManager) and it looks like it's maven-artifact artifact but it does not change anything.

Here's the test:

package com.example;

import java.io.File;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugin.testing.AbstractMojoTestCase;

public class MyMojoTest extends AbstractMojoTestCase
{

    private static final String PROJECT_TO_TEST_ROOT_PATH = "src/test/resources/unit/project-to-test";

    private static final String PROJECT_TO_TEST_ROOT_POM_PATH = PROJECT_TO_TEST_ROOT_PATH + "/pom.xml";

    public void testHistoryFilesAreGeneratedInFoldersWithScriptFiles() throws Exception
    {
        executeGenerateHistoryMojo(PROJECT_TO_TEST_ROOT_POM_PATH);
        assertHistoryFileGenerated("t1");
    }

    public void testHistoryFilesAreNotGeneratedInFoldersWithNoScriptFiles() throws Exception
    {
        executeGenerateHistoryMojo(PROJECT_TO_TEST_ROOT_POM_PATH);
        assertHistoryFileNotGenerated("t4");
    }

    private void assertHistoryFileGenerated(String relativePath)
    {
        String fullPath = PROJECT_TO_TEST_ROOT_PATH + "/" + relativePath;
        assertTrue("History file has not been generated in " + fullPath, doesHistoryFileExistIn(fullPath));
    }

    private void assertHistoryFileNotGenerated(String relativePath)
    {
        String fullPath = PROJECT_TO_TEST_ROOT_PATH + "/" + relativePath;
        assertTrue("History file has not been generated in " + fullPath, doesHistoryFileExistIn(fullPath));
    }

    private boolean doesHistoryFileExistIn(String string)
    {
        return false;
    }

    private void executeGenerateHistoryMojo(String directoryRoot) throws Exception, MojoExecutionException, MojoFailureException
    {
        File pom = getTestFile(directoryRoot);
        assertNotNull(pom);
        assertTrue(pom.exists());
        MyMojo myMojo = (MyMojo) lookupMojo("my-mojo", pom);
        assertNotNull(myMojo);
        myMojo.execute();
    }

}

And the plugin code:

package com.example;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.DirectoryFileFilter;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;

/**
 * @goal my-mojo
 * @execute phase="generate-resources"
 */
public class MyMojo extends AbstractMojo
{

    private final File historyFile = new File("history.log");

    /**
     * The currently executed project (can be a reactor project).
     * 
     * @parameter expression="${executedProject}"
     * @readonly
     */
    protected MavenProject executedProject;

    public void execute() throws MojoExecutionException, MojoFailureException
    {
        createHistoryFile();
        try
        {
            copyHistoryFileToAllProjectSubdirectoriesOf(executedProject
                .getBasedir());
        }
        catch (IOException e)
        {
            throw new MojoExecutionException("Could not create history files", e);
        }
    }

    private void createHistoryFile()
    {
        try
        {
            historyFile.createNewFile();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    /**
     * Copies {@link #historyFile} into all sub-directories of the provided directory which contain
     * script.java file.
     * 
     * @param pDirectory
     * @throws IOException
     */
    private void copyHistoryFileToAllProjectSubdirectoriesOf(File pDirectory)
        throws IOException
    {
        copyMasterClasspathToFolder(pDirectory);
        File[] directories = pDirectory
            .listFiles((FileFilter) DirectoryFileFilter.DIRECTORY);
        for (File subdirectory : directories)
        {
            copyMasterClasspathToFolder(subdirectory);
        }
    }

    /**
     * Copies {@link #historyFile} into the pDirectory if it contains a script.java file.
     * 
     * @param pDirectory
     * @throws IOException
     */
    private void copyMasterClasspathToFolder(File pDirectory)
        throws IOException
    {
        if (isScriptDirectory(pDirectory))
        {
            FileUtils.copyFileToDirectory(historyFile, pDirectory);
        }
    }

    /**
     * The method verifies if in the folder there is ".project" and "script.java" files.
     * 
     * @param pDirectory
     * @return true if pDirectory points to an OATS script file.
     */
    public static boolean isScriptDirectory(File pDirectory)
    {
        File projectFile = new File(pDirectory, ".project");
        File scriptFile = new File(pDirectory, "script.java");
        return scriptFile.exists() && projectFile.exists();
    }

    public void setExecutedProject(MavenProject executedProject)
    {
        this.executedProject = executedProject;
    }

}
like image 849
kboom Avatar asked Jan 16 '14 07:01

kboom


1 Answers

The problem comes from dependency conflicts between maven-plugin-api version 3.0.3 and maven-eclipse-plugin version 2.9. More specifically, maven-plugin-api pulls Maven dependencies for version 3.0.3 whereas maven-eclipse-plugin pulls Maven dependencies for version 2.0.8.

You actually do not need the maven-eclipse-plugin dependency and if you remove it, the error goes away:

<dependencies>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-plugin-api</artifactId>
        <version>3.0.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven</groupId>
        <artifactId>maven-compat</artifactId>
        <version>3.0.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-exec</artifactId>
        <version>1.1</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.maven.plugin-testing</groupId>
        <artifactId>maven-plugin-testing-harness</artifactId>
        <version>2.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Note that I also removed the explicit dependency on maven-core: you do not need to specify it because it is already a transitive dependency of maven-compat.

like image 62
Tunaki Avatar answered Oct 16 '22 15:10

Tunaki