Search in sources :

Example 26 with Resource

use of org.apache.maven.model.Resource in project maven-plugins by apache.

the class ResourcesMojoTest method testResourceEncoding.

/**
     * @throws Exception
     */
public void testResourceEncoding() throws Exception {
    File testPom = new File(getBasedir(), defaultPomFilePath);
    ResourcesMojo mojo = (ResourcesMojo) lookupMojo("resources", testPom);
    MavenProjectResourcesStub project = new MavenProjectResourcesStub("encoding");
    List<Resource> resources = project.getBuild().getResources();
    assertNotNull(mojo);
    project.addFile("file4.txt");
    project.setResourceFiltering(0, true);
    project.setupBuildEnvironment();
    setVariableValueToObject(mojo, "encoding", "UTF-8");
    setVariableValueToObject(mojo, "project", project);
    setVariableValueToObject(mojo, "resources", resources);
    setVariableValueToObject(mojo, "outputDirectory", new File(project.getBuild().getOutputDirectory()));
    setVariableValueToObject(mojo, "buildFilters", Collections.emptyList());
    setVariableValueToObject(mojo, "useBuildFilters", Boolean.TRUE);
    mojo.execute();
    String resourcesDir = project.getOutputDirectory();
    assertTrue(FileUtils.fileExists(resourcesDir + "/file4.txt"));
}
Also used : MavenProjectResourcesStub(org.apache.maven.plugins.resources.stub.MavenProjectResourcesStub) Resource(org.apache.maven.model.Resource) File(java.io.File)

Example 27 with Resource

use of org.apache.maven.model.Resource in project maven-plugins by apache.

the class ResourcesMojoTest method testResourceSystemProperties_Filtering.

/**
     * @throws Exception
     */
public void testResourceSystemProperties_Filtering() throws Exception {
    File testPom = new File(getBasedir(), defaultPomFilePath);
    ResourcesMojo mojo = (ResourcesMojo) lookupMojo("resources", testPom);
    MavenProjectResourcesStub project = new MavenProjectResourcesStub("resourceSystemProperties_Filtering");
    List<Resource> resources = project.getBuild().getResources();
    assertNotNull(mojo);
    project.addFile("file4.txt", "current-working-directory = ${user.dir}");
    project.setResourceFiltering(0, true);
    project.setupBuildEnvironment();
    // setVariableValueToObject(mojo,"encoding","UTF-8");
    setVariableValueToObject(mojo, "project", project);
    setVariableValueToObject(mojo, "resources", resources);
    setVariableValueToObject(mojo, "outputDirectory", new File(project.getBuild().getOutputDirectory()));
    setVariableValueToObject(mojo, "buildFilters", Collections.emptyList());
    setVariableValueToObject(mojo, "useBuildFilters", Boolean.TRUE);
    setVariableValueToObject(mojo, "escapeWindowsPaths", Boolean.TRUE);
    MavenExecutionRequest request = new DefaultMavenExecutionRequest();
    request.setSystemProperties(System.getProperties());
    MavenSession mavenSession = new MavenSession((PlexusContainer) null, (RepositorySystemSession) null, request, null);
    setVariableValueToObject(mojo, "session", mavenSession);
    mojo.execute();
    String resourcesDir = project.getOutputDirectory();
    File userDir = new File(System.getProperty("user.dir"));
    assertTrue(userDir.exists());
    Properties props = new Properties();
    final FileInputStream inStream = new FileInputStream(new File(resourcesDir, "file4.txt"));
    try {
        props.load(inStream);
    } finally {
        inStream.close();
    }
    File fileFromFiltering = new File(props.getProperty("current-working-directory"));
    assertTrue(fileFromFiltering.getAbsolutePath() + " does not exist.", fileFromFiltering.exists());
    assertEquals(userDir.getAbsolutePath(), fileFromFiltering.getAbsolutePath());
}
Also used : MavenProjectResourcesStub(org.apache.maven.plugins.resources.stub.MavenProjectResourcesStub) MavenSession(org.apache.maven.execution.MavenSession) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) Resource(org.apache.maven.model.Resource) Properties(java.util.Properties) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 28 with Resource

use of org.apache.maven.model.Resource in project maven-plugins by apache.

the class ResourcesMojoTest method testProjectProperty_Filtering_PropertyDestination.

/**
     * @throws Exception
     */
public void testProjectProperty_Filtering_PropertyDestination() throws Exception {
    File testPom = new File(getBasedir(), defaultPomFilePath);
    ResourcesMojo mojo = (ResourcesMojo) lookupMojo("resources", testPom);
    MavenProjectResourcesStub project = new MavenProjectResourcesStub("resourcePojectProperty_Filtering_PropertyDestination");
    List<Resource> resources = project.getBuild().getResources();
    assertNotNull(mojo);
    project.addFile("file4.properties", "current working directory=${description}");
    project.setResourceFiltering(0, true);
    project.setupBuildEnvironment();
    // setup dummy property
    project.setDescription("c:\\\\org\\apache\\test");
    // setVariableValueToObject(mojo,"encoding","UTF-8");
    setVariableValueToObject(mojo, "project", project);
    setVariableValueToObject(mojo, "resources", resources);
    setVariableValueToObject(mojo, "outputDirectory", new File(project.getBuild().getOutputDirectory()));
    setVariableValueToObject(mojo, "buildFilters", Collections.emptyList());
    setVariableValueToObject(mojo, "useBuildFilters", Boolean.TRUE);
    setVariableValueToObject(mojo, "escapeWindowsPaths", Boolean.TRUE);
    mojo.execute();
    String resourcesDir = project.getOutputDirectory();
    String checkString = "current working directory=c:\\\\org\\\\apache\\\\test";
    assertContent(resourcesDir + "/file4.properties", checkString);
}
Also used : MavenProjectResourcesStub(org.apache.maven.plugins.resources.stub.MavenProjectResourcesStub) Resource(org.apache.maven.model.Resource) File(java.io.File)

Example 29 with Resource

use of org.apache.maven.model.Resource in project maven-plugins by apache.

the class ResourcesMojoTest method testResourceDirectoryStructure.

/**
     * @throws Exception
     */
public void testResourceDirectoryStructure() throws Exception {
    File testPom = new File(getBasedir(), defaultPomFilePath);
    ResourcesMojo mojo = (ResourcesMojo) lookupMojo("resources", testPom);
    MavenProjectResourcesStub project = new MavenProjectResourcesStub("resourceDirectoryStructure");
    List<Resource> resources = project.getBuild().getResources();
    assertNotNull(mojo);
    project.addFile("file4.txt");
    project.addFile("package/file3.nottest");
    project.addFile("notpackage/file1.include");
    project.addFile("package/test/file1.txt");
    project.addFile("notpackage/test/file2.txt");
    project.setupBuildEnvironment();
    setVariableValueToObject(mojo, "project", project);
    setVariableValueToObject(mojo, "resources", resources);
    setVariableValueToObject(mojo, "outputDirectory", new File(project.getBuild().getOutputDirectory()));
    setVariableValueToObject(mojo, "buildFilters", Collections.emptyList());
    setVariableValueToObject(mojo, "useBuildFilters", Boolean.TRUE);
    mojo.execute();
    String resourcesDir = project.getOutputDirectory();
    assertTrue(FileUtils.fileExists(resourcesDir + "/file4.txt"));
    assertTrue(FileUtils.fileExists(resourcesDir + "/package/file3.nottest"));
    assertTrue(FileUtils.fileExists(resourcesDir + "/notpackage/file1.include"));
    assertTrue(FileUtils.fileExists(resourcesDir + "/package/test"));
    assertTrue(FileUtils.fileExists(resourcesDir + "/notpackage/test"));
}
Also used : MavenProjectResourcesStub(org.apache.maven.plugins.resources.stub.MavenProjectResourcesStub) Resource(org.apache.maven.model.Resource) File(java.io.File)

Example 30 with Resource

use of org.apache.maven.model.Resource in project maven-plugins by apache.

the class AntBuildWriter method writeBuildProperties.

/**
     * Generate <code>maven-build.properties</code> only for a non-POM project
     *
     * @throws IOException In case of an failure {@link IOException}
     * @see #DEFAULT_MAVEN_PROPERTIES_FILENAME
     */
protected void writeBuildProperties() throws IOException {
    if (AntBuildWriterUtil.isPomPackaging(project)) {
        return;
    }
    Properties properties = new Properties();
    // ----------------------------------------------------------------------
    // Build properties
    // ----------------------------------------------------------------------
    addProperty(properties, "maven.build.finalName", AntBuildWriterUtil.toRelative(project.getBasedir(), project.getBuild().getFinalName()));
    // target
    addProperty(properties, "maven.build.dir", AntBuildWriterUtil.toRelative(project.getBasedir(), project.getBuild().getDirectory()));
    addProperty(properties, "project.build.directory", "${maven.build.dir}");
    // ${maven.build.dir}/classes
    addProperty(properties, "maven.build.outputDir", "${maven.build.dir}/" + AntBuildWriterUtil.toRelative(new File(project.getBasedir(), properties.getProperty("maven.build.dir")), project.getBuild().getOutputDirectory()));
    addProperty(properties, "project.build.outputDirectory", "${maven.build.outputDir}");
    // src/main/java
    if (!project.getCompileSourceRoots().isEmpty()) {
        List var = project.getCompileSourceRoots();
        String[] compileSourceRoots = (String[]) var.toArray(new String[var.size()]);
        for (int i = 0; i < compileSourceRoots.length; i++) {
            addProperty(properties, "maven.build.srcDir." + i, AntBuildWriterUtil.toRelative(project.getBasedir(), compileSourceRoots[i]));
        }
    }
    // src/main/resources
    if (project.getBuild().getResources() != null) {
        List<Resource> var = project.getBuild().getResources();
        Resource[] array = var.toArray(new Resource[var.size()]);
        for (int i = 0; i < array.length; i++) {
            addProperty(properties, "maven.build.resourceDir." + i, AntBuildWriterUtil.toRelative(project.getBasedir(), array[i].getDirectory()));
        }
    }
    // ${maven.build.dir}/test-classes
    addProperty(properties, "maven.build.testOutputDir", "${maven.build.dir}/" + AntBuildWriterUtil.toRelative(new File(project.getBasedir(), properties.getProperty("maven.build.dir")), project.getBuild().getTestOutputDirectory()));
    // src/test/java
    if (!project.getTestCompileSourceRoots().isEmpty()) {
        List var = project.getTestCompileSourceRoots();
        String[] compileSourceRoots = (String[]) var.toArray(new String[var.size()]);
        for (int i = 0; i < compileSourceRoots.length; i++) {
            addProperty(properties, "maven.build.testDir." + i, AntBuildWriterUtil.toRelative(project.getBasedir(), compileSourceRoots[i]));
        }
    }
    // src/test/resources
    if (project.getBuild().getTestResources() != null) {
        List<Resource> var = project.getBuild().getTestResources();
        Resource[] array = var.toArray(new Resource[var.size()]);
        for (int i = 0; i < array.length; i++) {
            addProperty(properties, "maven.build.testResourceDir." + i, AntBuildWriterUtil.toRelative(project.getBasedir(), array[i].getDirectory()));
        }
    }
    addProperty(properties, "maven.test.reports", "${maven.build.dir}/test-reports");
    addProperty(properties, "maven.reporting.outputDirectory", "${maven.build.dir}/site");
    // ----------------------------------------------------------------------
    // Settings properties
    // ----------------------------------------------------------------------
    addProperty(properties, "maven.settings.offline", String.valueOf(settings.isOffline()));
    addProperty(properties, "maven.settings.interactiveMode", String.valueOf(settings.isInteractiveMode()));
    addProperty(properties, "maven.repo.local", getLocalRepositoryPath());
    if (project.getProperties() != null) {
        for (Map.Entry<Object, Object> objectObjectEntry : project.getProperties().entrySet()) {
            Map.Entry property = (Map.Entry) objectObjectEntry;
            addProperty(properties, property.getKey().toString(), property.getValue().toString());
        }
    }
    FileOutputStream os = new FileOutputStream(new File(project.getBasedir(), DEFAULT_MAVEN_PROPERTIES_FILENAME));
    try {
        properties.store(os, "Generated by Maven Ant Plugin - DO NOT EDIT THIS FILE!");
        os.close();
        os = null;
    } finally {
        IOUtil.close(os);
    }
}
Also used : Resource(org.apache.maven.model.Resource) Properties(java.util.Properties) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Aggregations

Resource (org.apache.maven.model.Resource)65 File (java.io.File)49 MavenProjectResourcesStub (org.apache.maven.plugins.resources.stub.MavenProjectResourcesStub)17 ArrayList (java.util.ArrayList)16 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)15 IOException (java.io.IOException)11 Properties (java.util.Properties)8 MojoFailureException (org.apache.maven.plugin.MojoFailureException)8 FileInputStream (java.io.FileInputStream)7 List (java.util.List)6 InputStream (java.io.InputStream)5 OutputStream (java.io.OutputStream)5 URL (java.net.URL)5 LinkedList (java.util.LinkedList)4 Map (java.util.Map)4 URLClassLoader (java.net.URLClassLoader)3 Iterator (java.util.Iterator)3 FileOutputStream (java.io.FileOutputStream)2 MalformedURLException (java.net.MalformedURLException)2 HashMap (java.util.HashMap)2