最新消息:天气越来越冷,记得加一件厚衣裳

Maven集成 – MavenCli

Java w3sun 2462浏览 0评论

简介

Maven Embedder是Maven的一部分,属于一类嵌入式组件,支持CLI和日志记录功能。官网资料比较少,只有很短的几句介绍,如果不需要在构建过程中进行比较精细的监控和步骤跟踪,可以考虑这种集成方式。此外,Maven Embedder有很多直接和传递依赖,在实际使用中需要注意。

参考链接:

http://maven.apache.org/ref/3.5.2/maven-embedder/

https://github.com/apache/maven/tree/master/maven-embedder

依赖

<dependency>
  <groupId>org.apache.maven</groupId>
  <artifactId>maven-embedder</artifactId>
  <version>3.0.5</version>
</dependency>

Maven集成

为了使用或者测试方便,将构建方式包装了一小下:

package com.w3sun.it.maven;

import java.io.PrintStream;

import org.apache.maven.cli.MavenCli;

/**
 * @author: w3sun
 * @date: 2018/12/16 19:50
 * @description:
 */
public class MavenCliBuilder {
  private MavenCli mavenCli;
  private String[] args;
  private PrintStream stdout;
  private PrintStream stderr;
  private String workingDirectory = System.getProperty("user.dir");


  public MavenCliBuilder() {
    this.mavenCli = new MavenCli();
    this.stdout = System.out;
    this.stderr = System.err;
    this.args = new String[]{"clean", "package", "-Dmaven.test.skip=true"};
    System.out.println(workingDirectory);
  }

  public MavenCliBuilder(String workingDirectory, PrintStream stdout, PrintStream stderr,
                         String... args) {
    this.mavenCli = new MavenCli();
    this.workingDirectory = workingDirectory;
    this.stdout = stdout;
    this.stderr = stderr;
    this.args = args;
  }

  public MavenCli getMavenCli() {
    return mavenCli;
  }

  public String[] getArgs() {
    return args;
  }

  public void setArgs(String[] args) {
    this.args = args;
  }

  public PrintStream getStdout() {
    return stdout;
  }

  public void setStdout(PrintStream stdout) {
    this.stdout = stdout;
  }

  public PrintStream getStderr() {
    return stderr;
  }

  public void setStderr(PrintStream stderr) {
    this.stderr = stderr;
  }

  public String getWorkingDirectory() {
    return workingDirectory;
  }

  public void setWorkingDirectory(String workingDirectory) {
    this.workingDirectory = workingDirectory;
  }

  public void build(){
    mavenCli.doMain(args,workingDirectory,stdout,stderr);
  }
}

针对上述构建方式采用了三种方法进行测试,分别为无参数,带参数和输出结果重定向,变相来讲只要能拿到执行时的输出就可以选择将其打印到标准输出还是通过logger记录下来:

package com.w3sun.it.maven;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author: w3sun
 * @date: 2018/12/16 21:30
 * @description:
 */
public class MavenCliBuilderTest {
  private static final Logger logger = LoggerFactory.getLogger(MavenCliBuilder.class);

  @Test
  public void buildWithoutParams() {
    MavenCliBuilder mavenCliBuilder = new MavenCliBuilder();
    mavenCliBuilder.build();
  }

  @Test
  public void buildWithParams() throws FileNotFoundException {
    MavenCliBuilder mavenCliBuilder = new MavenCliBuilder();
    PrintStream stdout = new PrintStream(new File("tst.dat"));
    mavenCliBuilder.setStderr(stdout);
    PrintStream stderr = new PrintStream(new File("tst.dat"));
    mavenCliBuilder.setStdout(stderr);
    mavenCliBuilder.build();
    stdout.close();
    stderr.close();
  }

  @Test
  public void buildWithLogger() throws IOException {
    MavenCliBuilder mavenCliBuilder = new MavenCliBuilder();
//    mavenCliBuilder.setArgs(new String[]{"aaa", "bbb"});
    ByteArrayOutputStream baosStdout = new ByteArrayOutputStream();
    PrintStream printStreamStdout = new PrintStream(baosStdout);
    mavenCliBuilder.setStdout(printStreamStdout);
    mavenCliBuilder.build();

    String out = new String(baosStdout.toByteArray(), "UTF-8");
    System.out.println("===========this is separator==============");
    System.out.println(out);
    baosStdout.close();
  }
}

效果

转载请注明:雪后西塘 » Maven集成 – MavenCli

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址