Search in sources :

Example 56 with MavenProject

use of org.apache.maven.project.MavenProject in project maven-plugins by apache.

the class ShadeMojoTest method testShadeWithFilter.

/**
     * Tests if a Filter is installed correctly, also if createSourcesJar is set to true.
     *
     * @throws Exception
     */
public void testShadeWithFilter() throws Exception {
    ShadeMojo mojo = new ShadeMojo();
    // set createSourcesJar = true
    Field createSourcesJar = ShadeMojo.class.getDeclaredField("createSourcesJar");
    createSourcesJar.setAccessible(true);
    createSourcesJar.set(mojo, Boolean.TRUE);
    // configure artifactResolver (mocked) for mojo
    ArtifactResolver mockArtifactResolver = new ArtifactResolver() {

        @Override
        public ArtifactResult resolveArtifact(ProjectBuildingRequest req, final Artifact art) throws ArtifactResolverException {
            return new ArtifactResult() {

                @Override
                public Artifact getArtifact() {
                    art.setResolved(true);
                    String fileName = art.getArtifactId() + "-" + art.getVersion() + (art.getClassifier() != null ? "-" + art.getClassifier() : "") + ".jar";
                    art.setFile(new File(fileName));
                    return art;
                }
            };
        }

        @Override
        public ArtifactResult resolveArtifact(ProjectBuildingRequest req, final ArtifactCoordinate coordinate) throws ArtifactResolverException {
            return new ArtifactResult() {

                @Override
                public Artifact getArtifact() {
                    Artifact art = mock(Artifact.class);
                    when(art.getGroupId()).thenReturn(coordinate.getGroupId());
                    when(art.getArtifactId()).thenReturn(coordinate.getArtifactId());
                    when(art.getType()).thenReturn(coordinate.getExtension());
                    when(art.getClassifier()).thenReturn(coordinate.getClassifier());
                    when(art.isResolved()).thenReturn(true);
                    String fileName = coordinate.getArtifactId() + "-" + coordinate.getVersion() + (coordinate.getClassifier() != null ? "-" + coordinate.getClassifier() : "") + ".jar";
                    when(art.getFile()).thenReturn(new File(fileName));
                    return art;
                }
            };
        }
    };
    Field artifactResolverField = ShadeMojo.class.getDeclaredField("artifactResolver");
    artifactResolverField.setAccessible(true);
    artifactResolverField.set(mojo, mockArtifactResolver);
    // create and configure MavenProject
    MavenProject project = new MavenProject();
    ArtifactHandler artifactHandler = (ArtifactHandler) lookup(ArtifactHandler.ROLE);
    Artifact artifact = new DefaultArtifact("org.apache.myfaces.core", "myfaces-impl", VersionRange.createFromVersion("2.0.1-SNAPSHOT"), "compile", "jar", null, artifactHandler);
    // setFile and setResolved
    artifact = mockArtifactResolver.resolveArtifact(null, artifact).getArtifact();
    project.setArtifact(artifact);
    Field projectField = ShadeMojo.class.getDeclaredField("project");
    projectField.setAccessible(true);
    projectField.set(mojo, project);
    // create and configure the ArchiveFilter
    ArchiveFilter archiveFilter = new ArchiveFilter();
    Field archiveFilterArtifact = ArchiveFilter.class.getDeclaredField("artifact");
    archiveFilterArtifact.setAccessible(true);
    archiveFilterArtifact.set(archiveFilter, "org.apache.myfaces.core:myfaces-impl");
    // add ArchiveFilter to mojo
    Field filtersField = ShadeMojo.class.getDeclaredField("filters");
    filtersField.setAccessible(true);
    filtersField.set(mojo, new ArchiveFilter[] { archiveFilter });
    Field sessionField = ShadeMojo.class.getDeclaredField("session");
    sessionField.setAccessible(true);
    sessionField.set(mojo, mock(MavenSession.class));
    // invoke getFilters()
    Method getFilters = ShadeMojo.class.getDeclaredMethod("getFilters", new Class[0]);
    getFilters.setAccessible(true);
    List<Filter> filters = (List<Filter>) getFilters.invoke(mojo);
    // assertions - there must be one filter
    assertEquals(1, filters.size());
    // the filter must be able to filter the binary and the sources jar
    Filter filter = filters.get(0);
    // binary jar
    assertTrue(filter.canFilter(new File("myfaces-impl-2.0.1-SNAPSHOT.jar")));
    // sources jar
    assertTrue(filter.canFilter(new File("myfaces-impl-2.0.1-SNAPSHOT-sources.jar")));
}
Also used : ArtifactCoordinate(org.apache.maven.shared.artifact.ArtifactCoordinate) Method(java.lang.reflect.Method) ArtifactResolver(org.apache.maven.shared.artifact.resolve.ArtifactResolver) Artifact(org.apache.maven.artifact.Artifact) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) ArtifactResult(org.apache.maven.shared.artifact.resolve.ArtifactResult) Field(java.lang.reflect.Field) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) MavenSession(org.apache.maven.execution.MavenSession) ArtifactHandler(org.apache.maven.artifact.handler.ArtifactHandler) MavenProject(org.apache.maven.project.MavenProject) Filter(org.apache.maven.plugins.shade.filter.Filter) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact)

Example 57 with MavenProject

use of org.apache.maven.project.MavenProject in project maven-plugins by apache.

the class AddDependencySetsTaskTest method testGetDependencyArtifacts_ShouldFilterOneDependencyArtifactViaInclude.

public void testGetDependencyArtifacts_ShouldFilterOneDependencyArtifactViaInclude() throws ArchiveCreationException, InvalidAssemblerConfigurationException {
    final MavenProject project = new MavenProject(new Model());
    final Set<Artifact> artifacts = new HashSet<Artifact>();
    final ArtifactMock am = new ArtifactMock(mockManager, "group", "artifact", "1.0", "jar", false);
    am.setDependencyTrail(Collections.singletonList(project.getId()));
    artifacts.add(am.getArtifact());
    final ArtifactMock am2 = new ArtifactMock(mockManager, "group2", "artifact2", "1.0", "jar", false);
    am2.setDependencyTrail(Collections.singletonList(project.getId()));
    artifacts.add(am2.getArtifact());
    final DependencySet dependencySet = new DependencySet();
    dependencySet.addInclude("group:artifact");
    dependencySet.setUseTransitiveFiltering(true);
    final Logger logger = new ConsoleLogger(Logger.LEVEL_DEBUG, "test");
    mockManager.replayAll();
    final AddDependencySetsTask task = new AddDependencySetsTask(Collections.singletonList(dependencySet), artifacts, project, null, logger);
    final Set<Artifact> result = task.resolveDependencyArtifacts(dependencySet);
    assertNotNull(result);
    assertEquals(1, result.size());
    assertSame(am.getArtifact(), result.iterator().next());
    mockManager.verifyAll();
}
Also used : MavenProject(org.apache.maven.project.MavenProject) ConsoleLogger(org.codehaus.plexus.logging.console.ConsoleLogger) Model(org.apache.maven.model.Model) DependencySet(org.apache.maven.plugins.assembly.model.DependencySet) ArtifactMock(org.apache.maven.plugins.assembly.archive.task.testutils.ArtifactMock) ConsoleLogger(org.codehaus.plexus.logging.console.ConsoleLogger) Logger(org.codehaus.plexus.logging.Logger) Artifact(org.apache.maven.artifact.Artifact) HashSet(java.util.HashSet) MockAndControlForAddDependencySetsTask(org.apache.maven.plugins.assembly.archive.task.testutils.MockAndControlForAddDependencySetsTask)

Example 58 with MavenProject

use of org.apache.maven.project.MavenProject in project maven-plugins by apache.

the class AddFileSetsTaskTest method testAddFileSet_ShouldNotAddDirectoryWhenSourceDirNonExistent.

public void testAddFileSet_ShouldNotAddDirectoryWhenSourceDirNonExistent() throws ArchiveCreationException, AssemblyFormattingException {
    final FileSet fs = new FileSet();
    final String dirname = "dir";
    fs.setDirectory(dirname);
    final File archiveBaseDir = fileManager.createTempDir();
    macTask.expectGetFinalName("finalName");
    //macTask.expectGetProject( null );
    expect(macTask.archiver.getOverrideDirectoryMode()).andReturn(-1);
    expect(macTask.archiver.getOverrideFileMode()).andReturn(-1);
    final MavenProject project = new MavenProject(new Model());
    DefaultAssemblyArchiverTest.setupInterpolators(macTask.configSource);
    mockManager.replayAll();
    final AddFileSetsTask task = new AddFileSetsTask(new ArrayList<FileSet>());
    task.setLogger(new ConsoleLogger(Logger.LEVEL_DEBUG, "test"));
    task.setProject(project);
    task.addFileSet(fs, macTask.archiver, macTask.configSource, archiveBaseDir);
    mockManager.verifyAll();
}
Also used : FileSet(org.apache.maven.plugins.assembly.model.FileSet) MavenProject(org.apache.maven.project.MavenProject) ConsoleLogger(org.codehaus.plexus.logging.console.ConsoleLogger) Model(org.apache.maven.model.Model) MockAndControlForAddFileSetsTask(org.apache.maven.plugins.assembly.archive.task.testutils.MockAndControlForAddFileSetsTask) File(java.io.File)

Example 59 with MavenProject

use of org.apache.maven.project.MavenProject in project maven-plugins by apache.

the class DefaultAssemblyReaderTest method testGetAssemblyFromDescriptorFile_ShouldReadAssembly.

public void testGetAssemblyFromDescriptorFile_ShouldReadAssembly() throws IOException, AssemblyReadException, InvalidAssemblerConfigurationException {
    final Assembly assembly = new Assembly();
    assembly.setId("test");
    final FileSet fs = new FileSet();
    fs.setDirectory("/dir");
    assembly.addFileSet(fs);
    final File assemblyFile = fileManager.createTempFile();
    final File basedir = assemblyFile.getParentFile();
    expect(configSource.getBasedir()).andReturn(basedir).anyTimes();
    expect(configSource.getProject()).andReturn(new MavenProject(new Model())).anyTimes();
    DefaultAssemblyArchiverTest.setupInterpolators(configSource);
    Writer writer = null;
    try {
        writer = new OutputStreamWriter(new FileOutputStream(assemblyFile), "UTF-8");
        new AssemblyXpp3Writer().write(writer, assembly);
        writer.close();
        writer = null;
    } finally {
        IOUtil.close(writer);
    }
    mockManager.replayAll();
    final Assembly result = new DefaultAssemblyReader().getAssemblyFromDescriptorFile(assemblyFile, configSource);
    assertEquals(assembly.getId(), result.getId());
    mockManager.verifyAll();
}
Also used : FileSet(org.apache.maven.plugins.assembly.model.FileSet) MavenProject(org.apache.maven.project.MavenProject) AssemblyXpp3Writer(org.apache.maven.plugins.assembly.model.io.xpp3.AssemblyXpp3Writer) FileOutputStream(java.io.FileOutputStream) Model(org.apache.maven.model.Model) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) Assembly(org.apache.maven.plugins.assembly.model.Assembly) OutputStreamWriter(java.io.OutputStreamWriter) StringWriter(java.io.StringWriter) AssemblyXpp3Writer(org.apache.maven.plugins.assembly.model.io.xpp3.AssemblyXpp3Writer) Writer(java.io.Writer) ComponentXpp3Writer(org.apache.maven.plugins.assembly.model.io.xpp3.ComponentXpp3Writer)

Example 60 with MavenProject

use of org.apache.maven.project.MavenProject in project maven-plugins by apache.

the class DefaultAssemblyReaderTest method performReadAssemblies.

private List<Assembly> performReadAssemblies(final File basedir, final String[] descriptors, final String[] descriptorRefs, final File descriptorDir, final boolean ignoreMissing) throws AssemblyReadException, InvalidAssemblerConfigurationException {
    expect(configSource.getDescriptorReferences()).andReturn(descriptorRefs);
    expect(configSource.getDescriptors()).andReturn(descriptors);
    expect(configSource.getDescriptorSourceDirectory()).andReturn(descriptorDir);
    expect(configSource.getBasedir()).andReturn(basedir).anyTimes();
    expect(configSource.getProject()).andReturn(new MavenProject(new Model())).anyTimes();
    expect(configSource.isIgnoreMissingDescriptor()).andReturn(ignoreMissing).anyTimes();
    DefaultAssemblyArchiverTest.setupInterpolators(configSource);
    mockManager.replayAll();
    final List<Assembly> assemblies = new DefaultAssemblyReader().readAssemblies(configSource);
    mockManager.verifyAll();
    return assemblies;
}
Also used : MavenProject(org.apache.maven.project.MavenProject) Model(org.apache.maven.model.Model) Assembly(org.apache.maven.plugins.assembly.model.Assembly)

Aggregations

MavenProject (org.apache.maven.project.MavenProject)297 File (java.io.File)138 Artifact (org.apache.maven.artifact.Artifact)66 ArrayList (java.util.ArrayList)64 Model (org.apache.maven.model.Model)57 ConsoleLogger (org.codehaus.plexus.logging.console.ConsoleLogger)36 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)33 IOException (java.io.IOException)29 Assembly (org.apache.maven.plugins.assembly.model.Assembly)28 EasyMockSupport (org.easymock.classextension.EasyMockSupport)27 ArtifactMock (org.apache.maven.plugins.assembly.archive.task.testutils.ArtifactMock)20 Test (org.junit.Test)17 HashMap (java.util.HashMap)16 HashSet (java.util.HashSet)16 MavenSession (org.apache.maven.execution.MavenSession)16 DependencySet (org.apache.maven.plugins.assembly.model.DependencySet)16 ProjectBuildingException (org.apache.maven.project.ProjectBuildingException)16 LinkedHashSet (java.util.LinkedHashSet)15 ArtifactRepository (org.apache.maven.artifact.repository.ArtifactRepository)15 FileSet (org.apache.maven.plugins.assembly.model.FileSet)15