All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.htfv.maven.plugins.testing.MavenTestClassLoader Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2012 htfv (Aliaksei Lahachou)
 *
 * Licensed 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.
 */

package com.github.htfv.maven.plugins.testing;

import java.io.IOException;
import java.net.URL;
import java.util.Enumeration;

/**
 * Implements a class loader, which hides resources in the project output
 * directory. This is required in order to hide components from the Plexus
 * container. If it's not done, Plexus first loads extensions from the
 * classpath, then Maven loads extensions again from POM.
 *
 * @author htfv (Aliaksei Lahachou)
 */
public class MavenTestClassLoader extends ClassLoader
{
    private String outputDirectoryURI;

    public MavenTestClassLoader(final ClassLoader parent)
    {
        super(parent);

        //
        // We should read project properties here, because later it may cause
        // recursive call to the class loader and fail.
        //

        outputDirectoryURI = ProjectProperties.getInstance()
                .getOutputDirectory().toURI().toString();
    }

    @Override
    public Enumeration getResources(final String name) throws IOException
    {
        Enumeration resources = super.getResources(name);

        resources =  new AbstractFilteredEnumeration(resources)
        {
            @Override
            protected boolean shouldIncludeElement(final URL element)
            {
                return !element.toString().startsWith(outputDirectoryURI);
            }
        };

        return resources;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy