Search in sources :

Example 21 with Settings

use of org.apache.maven.settings.Settings in project maven-plugins by apache.

the class EvaluateMojo method handleResponse.

/**
     * @param expr the user expression asked.
     * @param output the file where to write the result, or <code>null</code> to print in standard output.
     * @throws MojoExecutionException if any
     * @throws MojoFailureException if any reflection exceptions occur or missing components.
     */
private void handleResponse(String expr, File output) throws MojoExecutionException, MojoFailureException {
    StringBuilder response = new StringBuilder();
    Object obj;
    try {
        obj = getEvaluator().evaluate(expr);
    } catch (ExpressionEvaluationException e) {
        throw new MojoExecutionException("Error when evaluating the Maven expression", e);
    }
    if (obj != null && expr.equals(obj.toString())) {
        getLog().warn("The Maven expression was invalid. Please use a valid expression.");
        return;
    }
    // handle null
    if (obj == null) {
        response.append("null object or invalid expression");
    } else // handle primitives objects
    if (obj instanceof String) {
        response.append(obj.toString());
    } else if (obj instanceof Boolean) {
        response.append(obj.toString());
    } else if (obj instanceof Byte) {
        response.append(obj.toString());
    } else if (obj instanceof Character) {
        response.append(obj.toString());
    } else if (obj instanceof Double) {
        response.append(obj.toString());
    } else if (obj instanceof Float) {
        response.append(obj.toString());
    } else if (obj instanceof Integer) {
        response.append(obj.toString());
    } else if (obj instanceof Long) {
        response.append(obj.toString());
    } else if (obj instanceof Short) {
        response.append(obj.toString());
    } else // handle specific objects
    if (obj instanceof File) {
        File f = (File) obj;
        response.append(f.getAbsolutePath());
    } else // handle Maven pom object
    if (obj instanceof MavenProject) {
        MavenProject projectAsked = (MavenProject) obj;
        StringWriter sWriter = new StringWriter();
        MavenXpp3Writer pomWriter = new MavenXpp3Writer();
        try {
            pomWriter.write(sWriter, projectAsked.getModel());
        } catch (IOException e) {
            throw new MojoExecutionException("Error when writing pom", e);
        }
        response.append(sWriter.toString());
    } else // handle Maven Settings object
    if (obj instanceof Settings) {
        Settings settingsAsked = (Settings) obj;
        StringWriter sWriter = new StringWriter();
        SettingsXpp3Writer settingsWriter = new SettingsXpp3Writer();
        try {
            settingsWriter.write(sWriter, settingsAsked);
        } catch (IOException e) {
            throw new MojoExecutionException("Error when writing settings", e);
        }
        response.append(sWriter.toString());
    } else {
        // others Maven objects
        response.append(toXML(expr, obj));
    }
    if (output != null) {
        try {
            writeFile(output, response);
        } catch (IOException e) {
            throw new MojoExecutionException("Cannot write evaluation of expression to output: " + output, e);
        }
        getLog().info("Result of evaluation written to: " + output);
    } else {
        getLog().info(LS + response.toString());
    }
}
Also used : ExpressionEvaluationException(org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException) MavenProject(org.apache.maven.project.MavenProject) StringWriter(java.io.StringWriter) File(java.io.File) Settings(org.apache.maven.settings.Settings) MavenXpp3Writer(org.apache.maven.model.io.xpp3.MavenXpp3Writer) SettingsXpp3Writer(org.apache.maven.settings.io.xpp3.SettingsXpp3Writer)

Example 22 with Settings

use of org.apache.maven.settings.Settings in project maven-plugins by apache.

the class GitHubDownloaderTestCase method testConfigureAuthenticationWithProblems.

public void testConfigureAuthenticationWithProblems() throws Exception {
    IssueManagement issueManagement = newGitHubIssueManagement();
    GitHubDownloader gitHubDownloader = newGitHubDownloader(issueManagement);
    Settings settings = new Settings();
    Server server = newServer("github-server");
    settings.addServer(server);
    SettingsDecrypter decrypter = mock(SettingsDecrypter.class);
    SettingsDecryptionResult result = mock(SettingsDecryptionResult.class);
    Log log = mock(Log.class);
    when(result.getProblems()).thenReturn(Arrays.<SettingsProblem>asList(new DefaultSettingsProblem("Ups " + server.getId(), Severity.ERROR, null, -1, -1, null)));
    when(result.getServer()).thenReturn(server);
    when(decrypter.decrypt(any(SettingsDecryptionRequest.class))).thenReturn(result);
    gitHubDownloader.configureAuthentication(decrypter, "github-server", settings, log);
    verify(log).error("Ups github-server", null);
    ArgumentCaptor<SettingsDecryptionRequest> argument = ArgumentCaptor.forClass(SettingsDecryptionRequest.class);
    verify(decrypter).decrypt(argument.capture());
    List<Server> servers = ((DefaultSettingsDecryptionRequest) argument.getValue()).getServers();
    assertEquals(1, servers.size());
    assertSame(server, servers.get(0));
}
Also used : DefaultSettingsProblem(org.apache.maven.settings.building.DefaultSettingsProblem) Server(org.apache.maven.settings.Server) SettingsDecrypter(org.apache.maven.settings.crypto.SettingsDecrypter) Log(org.apache.maven.plugin.logging.Log) DefaultSettingsDecryptionRequest(org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest) SettingsDecryptionResult(org.apache.maven.settings.crypto.SettingsDecryptionResult) DefaultSettingsDecryptionRequest(org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest) SettingsDecryptionRequest(org.apache.maven.settings.crypto.SettingsDecryptionRequest) IssueManagement(org.apache.maven.model.IssueManagement) Settings(org.apache.maven.settings.Settings)

Example 23 with Settings

use of org.apache.maven.settings.Settings in project maven-plugins by apache.

the class JavadocReportTest method testProxy.

/**
     * Method to test proxy support in the javadoc
     *
     * @throws Exception if any
     */
public void testProxy() throws Exception {
    Settings settings = new Settings();
    Proxy proxy = new Proxy();
    // dummy proxy
    proxy.setActive(true);
    proxy.setHost("127.0.0.1");
    proxy.setPort(80);
    proxy.setProtocol("http");
    proxy.setUsername("toto");
    proxy.setPassword("toto");
    proxy.setNonProxyHosts("www.google.com|*.somewhere.com");
    settings.addProxy(proxy);
    File testPom = new File(getBasedir(), "src/test/resources/unit/proxy-test/proxy-test-plugin-config.xml");
    JavadocReport mojo = lookupMojo(testPom);
    MavenSession session = spy(newMavenSession(mojo.project));
    ProjectBuildingRequest buildingRequest = mock(ProjectBuildingRequest.class);
    when(buildingRequest.getRemoteRepositories()).thenReturn(mojo.project.getRemoteArtifactRepositories());
    when(session.getProjectBuildingRequest()).thenReturn(buildingRequest);
    MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
    repositorySession.setLocalRepositoryManager(new SimpleLocalRepositoryManager(localRepo));
    when(buildingRequest.getRepositorySession()).thenReturn(repositorySession);
    when(session.getRepositorySession()).thenReturn(repositorySession);
    LegacySupport legacySupport = lookup(LegacySupport.class);
    legacySupport.setSession(session);
    setVariableValueToObject(mojo, "settings", settings);
    setVariableValueToObject(mojo, "session", session);
    mojo.execute();
    File commandLine = new File(getBasedir(), "target/test/unit/proxy-test/target/site/apidocs/javadoc." + (SystemUtils.IS_OS_WINDOWS ? "bat" : "sh"));
    assertTrue(FileUtils.fileExists(commandLine.getAbsolutePath()));
    String readed = readFile(commandLine);
    assertTrue(readed.contains("-J-Dhttp.proxySet=true"));
    assertTrue(readed.contains("-J-Dhttp.proxyHost=127.0.0.1"));
    assertTrue(readed.contains("-J-Dhttp.proxyPort=80"));
    assertTrue(readed.contains("-J-Dhttp.proxyUser=\\\"toto\\\""));
    assertTrue(readed.contains("-J-Dhttp.proxyPassword=\\\"toto\\\""));
    assertTrue(readed.contains("-J-Dhttp.nonProxyHosts=\\\"www.google.com|*.somewhere.com\\\""));
    File options = new File(getBasedir(), "target/test/unit/proxy-test/target/site/apidocs/options");
    assertTrue(FileUtils.fileExists(options.getAbsolutePath()));
    String optionsContent = readFile(options);
    // NO -link expected
    assertFalse(optionsContent.contains("-link"));
    // real proxy
    ProxyServer proxyServer = null;
    AuthAsyncProxyServlet proxyServlet;
    try {
        proxyServlet = new AuthAsyncProxyServlet();
        proxyServer = new ProxyServer(proxyServlet);
        proxyServer.start();
        settings = new Settings();
        proxy = new Proxy();
        proxy.setActive(true);
        proxy.setHost(proxyServer.getHostName());
        proxy.setPort(proxyServer.getPort());
        proxy.setProtocol("http");
        settings.addProxy(proxy);
        mojo = lookupMojo(testPom);
        setVariableValueToObject(mojo, "settings", settings);
        setVariableValueToObject(mojo, "session", session);
        mojo.execute();
        readed = readFile(commandLine);
        assertTrue(readed.contains("-J-Dhttp.proxySet=true"));
        assertTrue(readed.contains("-J-Dhttp.proxyHost=" + proxyServer.getHostName()));
        assertTrue(readed.contains("-J-Dhttp.proxyPort=" + proxyServer.getPort()));
        optionsContent = readFile(options);
    // -link expected
    // TODO: This got disabled for now!
    // This test fails since the last commit but I actually think it only ever worked by accident.
    // It did rely on a commons-logging-1.0.4.pom which got resolved by a test which did run previously.
    // But after updating to commons-logging.1.1.1 there is no pre-resolved artifact available in
    // target/local-repo anymore, thus the javadoc link info cannot get built and the test fails
    // I'll for now just disable this line of code, because the test as far as I can see _never_
    // did go upstream. The remoteRepository list used is always empty!.
    //
    //            assertTrue( optionsContent.contains( "-link 'http://commons.apache.org/logging/apidocs'" ) );
    } finally {
        if (proxyServer != null) {
            proxyServer.stop();
        }
    }
    // auth proxy
    Map<String, String> authentications = new HashMap<String, String>();
    authentications.put("foo", "bar");
    try {
        proxyServlet = new AuthAsyncProxyServlet(authentications);
        proxyServer = new ProxyServer(proxyServlet);
        proxyServer.start();
        settings = new Settings();
        proxy = new Proxy();
        proxy.setActive(true);
        proxy.setHost(proxyServer.getHostName());
        proxy.setPort(proxyServer.getPort());
        proxy.setProtocol("http");
        proxy.setUsername("foo");
        proxy.setPassword("bar");
        settings.addProxy(proxy);
        mojo = lookupMojo(testPom);
        setVariableValueToObject(mojo, "settings", settings);
        setVariableValueToObject(mojo, "session", session);
        mojo.execute();
        readed = readFile(commandLine);
        assertTrue(readed.contains("-J-Dhttp.proxySet=true"));
        assertTrue(readed.contains("-J-Dhttp.proxyHost=" + proxyServer.getHostName()));
        assertTrue(readed.contains("-J-Dhttp.proxyPort=" + proxyServer.getPort()));
        assertTrue(readed.contains("-J-Dhttp.proxyUser=\\\"foo\\\""));
        assertTrue(readed.contains("-J-Dhttp.proxyPassword=\\\"bar\\\""));
        optionsContent = readFile(options);
    // -link expected
    // see comment above (line 829)
    //             assertTrue( optionsContent.contains( "-link 'http://commons.apache.org/logging/apidocs'" ) );
    } finally {
        if (proxyServer != null) {
            proxyServer.stop();
        }
    }
}
Also used : LegacySupport(org.apache.maven.plugin.LegacySupport) HashMap(java.util.HashMap) AuthAsyncProxyServlet(org.apache.maven.plugin.javadoc.ProxyServer.AuthAsyncProxyServlet) MavenRepositorySystemSession(org.apache.maven.repository.internal.MavenRepositorySystemSession) MavenSession(org.apache.maven.execution.MavenSession) ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) Proxy(org.apache.maven.settings.Proxy) SimpleLocalRepositoryManager(org.sonatype.aether.impl.internal.SimpleLocalRepositoryManager) File(java.io.File) Settings(org.apache.maven.settings.Settings)

Example 24 with Settings

use of org.apache.maven.settings.Settings in project maven-plugins by apache.

the class AbstractSiteDeployWebDavTest method davDeployThruProxyWithoutAuthzInProxy.

@Test
public void davDeployThruProxyWithoutAuthzInProxy() throws Exception {
    FileUtils.cleanDirectory(siteTargetPath);
    SimpleDavServerHandler simpleDavServerHandler = new SimpleDavServerHandler(siteTargetPath);
    try {
        File pluginXmlFile = getTestFile("src/test/resources/unit/deploy-dav/pom.xml");
        AbstractMojo mojo = getMojo(pluginXmlFile);
        assertNotNull(mojo);
        SiteMavenProjectStub siteMavenProjectStub = new SiteMavenProjectStub("deploy-dav");
        // olamy, Note : toto is something like foo or bar for french folks :-)
        String siteUrl = "dav:http://toto.com/site/";
        siteMavenProjectStub.getDistributionManagement().getSite().setUrl(siteUrl);
        setVariableValueToObject(mojo, "project", siteMavenProjectStub);
        Settings settings = new Settings();
        Proxy proxy = new Proxy();
        //dummy proxy
        proxy.setActive(true);
        proxy.setHost("localhost");
        proxy.setPort(simpleDavServerHandler.getPort());
        proxy.setProtocol("http");
        proxy.setNonProxyHosts("www.google.com|*.somewhere.com");
        settings.addProxy(proxy);
        setVariableValueToObject(mojo, "settings", settings);
        MavenExecutionRequest request = new DefaultMavenExecutionRequest();
        request.setProxies(Arrays.asList(proxy));
        MavenSession mavenSession = new MavenSession(getContainer(), null, request, null);
        setVariableValueToObject(mojo, "mavenSession", mavenSession);
        File inputDirectory = new File("src/test/resources/unit/deploy-dav/target/site");
        setVariableValueToObject(mojo, "inputDirectory", inputDirectory);
        mojo.execute();
        assertContentInFiles();
        assertTrue(requestsContainsProxyUse(simpleDavServerHandler.httpRequests));
        for (HttpRequest rq : simpleDavServerHandler.httpRequests) {
            log.info(rq.toString());
        }
    } finally {
        simpleDavServerHandler.stop();
    }
}
Also used : HttpRequest(org.apache.maven.plugins.site.deploy.SimpleDavServerHandler.HttpRequest) SiteMavenProjectStub(org.apache.maven.plugins.site.stubs.SiteMavenProjectStub) MavenSession(org.apache.maven.execution.MavenSession) Proxy(org.apache.maven.settings.Proxy) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) DefaultMavenExecutionRequest(org.apache.maven.execution.DefaultMavenExecutionRequest) AbstractMojo(org.apache.maven.plugin.AbstractMojo) File(java.io.File) Settings(org.apache.maven.settings.Settings) Test(org.junit.Test)

Example 25 with Settings

use of org.apache.maven.settings.Settings in project maven-plugins by apache.

the class AbstractSiteDeployWebDavTest method noAuthzDavDeploy.

@Test
public void noAuthzDavDeploy() throws Exception {
    FileUtils.cleanDirectory(siteTargetPath);
    SimpleDavServerHandler simpleDavServerHandler = new SimpleDavServerHandler(siteTargetPath);
    try {
        File pomFile = getTestFile("src/test/resources/unit/deploy-dav/pom.xml");
        AbstractMojo mojo = getMojo(pomFile);
        assertNotNull(mojo);
        SiteMavenProjectStub siteMavenProjectStub = new SiteMavenProjectStub("deploy-dav");
        assertTrue("dav server port not available: " + simpleDavServerHandler.getPort(), simpleDavServerHandler.getPort() > 0);
        siteMavenProjectStub.getDistributionManagement().getSite().setUrl("dav:http://localhost:" + simpleDavServerHandler.getPort() + "/site/");
        setVariableValueToObject(mojo, "project", siteMavenProjectStub);
        Settings settings = new Settings();
        setVariableValueToObject(mojo, "settings", settings);
        File inputDirectory = new File("src/test/resources/unit/deploy-dav/target/site");
        setVariableValueToObject(mojo, "inputDirectory", inputDirectory);
        mojo.execute();
        assertContentInFiles();
        assertFalse(requestsContainsProxyUse(simpleDavServerHandler.httpRequests));
    } finally {
        simpleDavServerHandler.stop();
    }
}
Also used : SiteMavenProjectStub(org.apache.maven.plugins.site.stubs.SiteMavenProjectStub) AbstractMojo(org.apache.maven.plugin.AbstractMojo) File(java.io.File) Settings(org.apache.maven.settings.Settings) Test(org.junit.Test)

Aggregations

Settings (org.apache.maven.settings.Settings)27 File (java.io.File)15 Proxy (org.apache.maven.settings.Proxy)6 IOException (java.io.IOException)5 DefaultSettingsBuildingRequest (org.apache.maven.settings.building.DefaultSettingsBuildingRequest)5 MavenProject (org.apache.maven.project.MavenProject)4 Server (org.apache.maven.settings.Server)4 SettingsBuildingException (org.apache.maven.settings.building.SettingsBuildingException)4 SettingsBuildingRequest (org.apache.maven.settings.building.SettingsBuildingRequest)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 MavenSession (org.apache.maven.execution.MavenSession)3 AbstractMojo (org.apache.maven.plugin.AbstractMojo)3 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 SiteMavenProjectStub (org.apache.maven.plugins.site.stubs.SiteMavenProjectStub)3 SettingsDecryptionResult (org.apache.maven.settings.crypto.SettingsDecryptionResult)3 StringWriter (java.io.StringWriter)2 Properties (java.util.Properties)2 Maven2Gradle (org.gradle.buildinit.plugins.internal.maven.Maven2Gradle)2 MavenConversionException (org.gradle.buildinit.plugins.internal.maven.MavenConversionException)2