Tag Archives: Maven

Improvement to Maven 3.0.5

A default encoding section in POMs would be welcome.

http://jira.codehaus.org/browse/MNG-2216 (vote for it!)

Advertisements

How to Setup Proxies in Maven

Just change the <Maven Install Dir>/conf/settings.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <proxies>
    <proxy>
      <active>false</active> <!-- default -->
      <protocol>http</protocol> <!-- default -->
      <port>8080</port> <!-- default -->
      <host>host</host>
    </proxy>
  </proxies>
</settings>

How to Download with the Maven Central API

import java.io.InputStream;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;

@SuppressWarnings("serial")
@WebServlet(name = "SampleMavenCentralServlet", value = "/")
public final class SampleMavenCentralServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) {
    final Client client = ClientBuilder.newClient();

    final Response res = client.target("http://search.maven.org/remotecontent?filepath=br/eti/fernandoribeiro/mavencentralapi/mavencentralapi-sample/1.0/mavencentralapi-sample.pom").get();

    final InputStream in = res.readEntity(InputStream.class);

    ...

    client.close();
  }

}

How to Search with the Maven Central API

By Group or Artifact ID

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;

@SuppressWarnings("serial")
@WebServlet(name = "SampleMavenCentralServlet", value = "/")
public final class SampleMavenCentralServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
    final Client client = ClientBuider.newClient();

    final Response res = client.target("http://search.maven.org/solrsearch/select?q=sample&rows=20&wt=json").get();

    resp.getWriter().println(res.readEntity(String.class));

    client.close();
  }

}

By Group ID

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;

@SuppressWarnings("serial")
@WebServlet(name = "SampleMavenCentralServlet", value = "/")
public final class SampleMavenCentralServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
    final Client client = ClientBuilder.newClient();

    final Response res = client.target("http://search.maven.org/solrsearch/select?q=g:br.eti.fernandoribeiro.sample&rows=20&wt=json").get();

    resp.getWriter().println(res.readEntity(String.class));

    client.close();
  }

}

By Artifact ID

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;

@SuppressWarnings("serial")
@WebServlet(name = "SampleMavenCentralServlet", value = "/")
public final class SampleMavenCentralServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
    final Client client = ClientBuilder.newClient();

    final Response res = client.target("http://search.maven.org/solrsearch/select?q=a:mavencentralapi-sample&rows=20&wt=json").get();

    resp.getWriter().println(res.readEntity(String.class));

    client.close();
  }

}

By Group ID and Artifact ID

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;

@SuppressWarnings("serial")
@WebServlet(name = "SampleMavenCentralServlet", value = "/")
public final class SampleMavenCentralServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
    final Client client = ClientBuilder.newClient();

    final Response res = client.target("http://search.maven.org/solrsearch/select?q=g:br.eti.fernandoribeiro.sample%20AND%20a:mavencentralapi-sample&rows=20&wt=json").get();

    resp.getWriter().println(res.readEntity(String.class));

    client.close();
  }

}

By Group ID, Artifact ID, Version, Classifier and Packaging

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;

@SuppressWarnings("serial")
@WebServlet(name = "SampleMavenCentralServlet", value = "/")
public final class SampleMavenCentralServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
    final Client client = ClientBuilder.newClient();

    final Response res = client.target("http://search.maven.org/solrsearch/select?q=g:br.eti.fernandoribeiro.sample%20AND%20a:mavencentralapi-sample%20AND%20v:1.0%20AND%20I:javadoc%20AND%20p:jar&rows=20&wt=json").get();

    resp.getWriter().println(res.readEntity(String.class));

    client.close();
  }

}

By Class Name

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;

@SuppressWarnings("serial")
@WebServlet(name = "SampleMavenCentralServlet", value = "/")
public final class SampleMavenCentralServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
    final Client client = ClientBuilder.newClient();

    final Response res = client.target("http://search.maven.org/solrsearch/select?q=c:MavenCentralApiSample&rows=20&wt=json").get();

    resp.getWriter().println(res.readEntity(String.class));

    client.close();
  }

}

By FQCN

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;

@SuppressWarnings("serial")
@WebServlet(name = "SampleMavenCentralServlet", value = "/")
public final class SampleMavenCentralServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
    final Client client = ClientBuilder.newClient();

    final Response res = client.target("http://search.maven.org/solrsearch/select?q=fc:br.eti.fernandoribeiro.sample.mavencentralapi.MavenCentralApiSample&rows=20&wt=json").get();

    resp.getWriter().println(res.readEntity(String.class));

    client.close();
  }

}

By SHA-1 Checksum

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;

@SuppressWarnings("serial")
@WebServlet(name = "SampleMavenCentralServlet", value = "/")
public final class SampleMavenCentralServlet extends HttpServlet {

  @Override
  protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
    final Client client = ClientBuilder.newClient();

    final Response res = client.target("http://search.maven.org/solrsearch/select?q=1:35379fb6526fd019f331542b4e9ae2e566c57933&rows=20&wt=json").get();

    resp.getWriter().println(res.readEntity(String.class));

    client.close();
  }

}

How to Create a Plugin for Maven

1) Create a mojo for every goal:

import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

/**
 * Sample Goal Description
 */
@Mojo(name = "sample")
public final class SampleMojo {
  /**
   * Sample Parameter Description
   */
  @Parameter
  private String param;

  public void execute() {
  }

}

2) Build the plugin:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>br.eti.fernandoribeiro.sample</groupId>
  <artifactId>sample-plugin</artifactId>
  <version>1.0</version>
  <packaging>maven-plugin</packaging>
  <dependencies>
    <dependency>
      <groupId>org.apache.maven</groupId>
      <artifactId>maven-plugin-api</artifactId>
      <version>3.0.4</version>
    </dependency>
    <dependency>
      <groupId>org.apache.maven.plugin-tools</groupId>
      <artifactId>maven-plugin-annotations</artifactId>
      <version>3.2</version>
      <scope>provided</scope>
    </dependency>
    ...
  </dependencies>
  <build>
      <plugin>
        <artifactId>maven-plugin-plugin</artifactId>
        <version>3.2</version>
        <configuration>
          <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
        </configuration>
      </plugin>
      ...
    </plugins>
  </build>
</project>

3) Run the plugin in Maven:

mvn br.eti.fernandoribeiro.sample:sample-plugin:sample

You can find more details here.

How to Generate a JAX-RS Client with Apache CXF JAX-RS Maven Plugin

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>br.eti.fernandoribeiro.rsclient</groupId>
  <artifactId>samplersclient</artifactId>
  <version>1.0</version>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-wadl2java-plugin</artifactId>
        <version>2.7.1</version>
        <executions>
          <execution>
            <configuration>
              <wadlOptions>
                <wadlOption>
                  <wadl>src/main/wadl/sample.wadl</wadl>
                  <packagename>br.eti.fernandoribeiro.rsclient.sample</packagename> <!-- defaults to "application" -->
                </wadlOption>
              </wadlOptions>
            </configuration>
            <goals>
              <goal>wadl2java</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>