DHorse系列文章之maven打包
插件打包
这种方式是平时最常用的,首先要下载并安装maven环境,然后在被打包的项目中引入插件,有各种各样的打包插件,比如springboot自带插件:
<plugin>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-maven-plugin</artifactId>
	<version>2.5.6</version>
	<configuration>
		<classifier>execute</classifier>
		<mainClass>com.test.Application</mainClass>
	</configuration>
	<executions>
		<execution>
			<goals>
				<goal>repackage</goal>
			</goals>
		</execution>
	</executions>
</plugin>
再比如,通用的打包插件:
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-assembly-plugin</artifactId>
	<version>3.8.2</version>
	<configuration>
		<appendAssemblyId>false</appendAssemblyId>
		<descriptors>
			<descriptor>src/main/resources/assemble.xml</descriptor>
		</descriptors>
		<outputDirectory>../target</outputDirectory>
	</configuration>
	<executions>
		<execution>
			<id>make-assembly</id>
			<phase>package</phase>
			<goals>
				<goal>single</goal>
			</goals>
		</execution>
	</executions>
</plugin>
等等。然后再通过运行mvn clean package命令进行打包。
DHorse的技术选型
在《DHorse系列文章之镜像制作》这篇文章里已经说过,DHorse作为一个简单易用的DevOps开发平台,在一开始设计时就考虑到了对外部环境的依赖性。无论是从安装还是从使用的角度,都应该尽量减少对外部环境的依赖。
同样,打包时也应该去除对maven环境的依赖。那么,如何实现呢?
可以使用嵌入式maven插件maven-embedder来实现,首先在平台项目里引入依赖,如下:
<dependency>
	<groupId>org.apache.maven</groupId>
	<artifactId>maven-embedder</artifactId>
	<version>3.8.1</version>
</dependency>
<dependency>
	<groupId>org.apache.maven</groupId>
	<artifactId>maven-compat</artifactId>
	<version>3.8.1</version>
</dependency>
<dependency>
	<groupId>org.apache.maven.resolver</groupId>
	<artifactId>maven-resolver-connector-basic</artifactId>
	<version>1.7.1</version>
</dependency>
<dependency>
	<groupId>org.apache.maven.resolver</groupId>
	<artifactId>maven-resolver-transport-http</artifactId>
	<version>1.7.1</version>
</dependency>
运行如下代码,就可以对hello项目进行打包了:
String[] commands = new String[] { "clean", "package", "-Dmaven.test.skip" };
String pomPath = "D:/hello/pom.xml";
MavenCli cli = new MavenCli();
try {
	cli.doMain(commands, pomPath, System.out, System.out);
} catch (Exception e) {
	e.printStackTrace();
}
但是,一般情况下,我们通过maven的settings文件还会做一些配置,比如配置工作目录、nexus私服地址、Jdk版本、编码方式等等,如下:
<?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">
	<localRepository>C:/m2/repository</localRepository>
	<profiles>
		<profile>
			<id>myNexus</id>
			<repositories>
				<repository>
					<id>nexus</id>
					<name>nexus</name>
					<url>https://repo.maven.apache.org/maven2</url>
					<releases>
						<enabled>true</enabled>
					</releases>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
				</repository>
			</repositories>
			<pluginRepositories>
				<pluginRepository>
					<id>nexus</id>
					<name>nexus</name>
					<url>https://repo.maven.apache.org/maven2</url>
					<releases>
						<enabled>true</enabled>
					</releases>
					<snapshots>
						<enabled>true</enabled>
					</snapshots>
				</pluginRepository>
			</pluginRepositories>
		</profile>
		<profile>
			<id>java11</id>
			<activation>
				<activeByDefault>true</activeByDefault>
				<jdk>11</jdk>
			</activation>
			<properties>
				<maven.compiler.source>11</maven.compiler.source>
				<maven.compiler.target>11</maven.compiler.target>
				<maven.compiler.compilerVersion>11</maven.compiler.compilerVersion>
				<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
				<project.build.outputEncoding>UTF-8</project.build.outputEncoding>
			</properties>
		</profile>
	</profiles>
	<activeProfiles>
		<activeProfile>myNexus</activeProfile>
	</activeProfiles>
</settings>
通过查看MavenCli类发现,doMain(CliRequest cliRequest)方法有比较丰富的参数,CliRequest的代码如下:
package org.apache.maven.cli;
public class CliRequest
{
    String[] args;
    CommandLine commandLine;
    ClassWorld classWorld;
    String workingDirectory;
    File multiModuleProjectDirectory;
    boolean debug;
    boolean quiet;
    boolean showErrors = true;
    Properties userProperties = new Properties();
    Properties systemProperties = new Properties();
    MavenExecutionRequest request;
    CliRequest( String[] args, ClassWorld classWorld )
    {
        this.args = args;
        this.classWorld = classWorld;
        this.request = new DefaultMavenExecutionRequest();
    }
    public String[] getArgs()
    {
        return args;
    }
    public CommandLine getCommandLine()
    {
        return commandLine;
    }
    public ClassWorld getClassWorld()
    {
        return classWorld;
    }
    public String getWorkingDirectory()
    {
        return workingDirectory;
    }
    public File getMultiModuleProjectDirectory()
    {
        return multiModuleProjectDirectory;
    }
    public boolean isDebug()
    {
        return debug;
    }
    public boolean isQuiet()
    {
        return quiet;
    }
    public boolean isShowErrors()
    {
        return showErrors;
    }
    public Properties getUserProperties()
    {
        return userProperties;
    }
    public Properties getSystemProperties()
    {
        return systemProperties;
    }
    public MavenExecutionRequest getRequest()
    {
        return request;
    }
    public void setUserProperties( Properties properties )
    {
        this.userProperties.putAll( properties );
    }
}
可以看出,这些参数非常丰富,也许可以满足我们的需求,但是CliRequest只有一个默认修饰符的构造方法,也就说只有位于org.apache.maven.cli包下的类才有访问CliRequest构造方法的权限,我们可以在平台项目里新建一个包org.apache.maven.cli,然后再创建一个类(如:DefaultCliRequest)继承自CliRequest,然后实现一个public的构造方法,就可以在任何包里使用该类了,如下代码:
package org.apache.maven.cli;
import org.codehaus.plexus.classworlds.ClassWorld;
public class DefaultCliRequest extends CliRequest{
	public DefaultCliRequest(String[] args, ClassWorld classWorld) {
		super(args, classWorld);
	}
	public void setWorkingDirectory(String directory) {
		this.workingDirectory = directory;
	}
}
定义好参数类型DefaultCliRequest后,我们再来看看打包的代码:
public void doPackage() {
	String[] commands = new String[] { "clean", "package", "-Dmaven.test.skip" };
	DefaultCliRequest request = new DefaultCliRequest(commands, null);
	request.setWorkingDirectory("D:/hello/pom.xml");
	Repository repository = new Repository();
	repository.setId("nexus");
	repository.setName("nexus");
	repository.setUrl("https://repo.maven.apache.org/maven2");
	RepositoryPolicy policy = new RepositoryPolicy();
	policy.setEnabled(true);
	policy.setUpdatePolicy("always");
	policy.setChecksumPolicy("fail");
	repository.setReleases(policy);
	repository.setSnapshots(policy);
	String javaVesion = "11";
	Profile profile = new Profile();
	profile.setId("java11");
	Activation activation = new Activation();
	activation.setActiveByDefault(true);
	activation.setJdk(javaVesion);
	profile.setActivation(activation);
	profile.setRepositories(Arrays.asList(repository));
	profile.setPluginRepositories(Arrays.asList(repository));
	Properties properties = new Properties();
	properties.put("java.home", "D:/java/jdk-11.0.16.2");
	properties.put("java.version", javaVesion);
	properties.put("maven.compiler.source", javaVesion);
	properties.put("maven.compiler.target", javaVesion);
	properties.put("maven.compiler.compilerVersion", javaVesion);
	properties.put("project.build.sourceEncoding", "UTF-8");
	properties.put("project.reporting.outputEncoding", "UTF-8");
	profile.setProperties(properties);
	MavenExecutionRequest executionRequest = request.getRequest();
	executionRequest.setProfiles(Arrays.asList(profile));
	MavenCli cli = new MavenCli();
	try {
		cli.doMain(request);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
如果需要设置其他参数,也可以通过以上参数自行添加。
DHorse系列文章之maven打包的更多相关文章
- Maven进价:Maven构建系列文章
		Maven:基于Java平台的项目构建.依赖管理和项目信息管理. 1.构建 Maven标准化了构建过程 构建过程:编译.运行单元测试.生成文档.打包和部署 避免重复:设计.编码.文档.构建 2.依赖管 ... 
- Spring Boot 2.0系列文章(五):Spring Boot 2.0 项目源码结构预览
		关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/15/springboot2_code/ 项目结构 结构分析: Spring-boot-pr ... 
- MyBatis 源码分析系列文章导读
		1.本文速览 本篇文章是我为接下来的 MyBatis 源码分析系列文章写的一个导读文章.本篇文章从 MyBatis 是什么(what),为什么要使用(why),以及如何使用(how)等三个角度进行了说 ... 
- Idea开发环境中搭建Maven并且使用Maven打包部署程序
		1.配置Maven的环境变量 a.首先我们去maven官网下载Maven程序,解压到安装目录,如图所示: b.配置M2_HOME的环境变量,然后将该变量添加到Path中 备注:必须要有JAVA_HOM ... 
- 【Xamarin开发 Android 系列 13】 应用打包部署
		原文:[Xamarin开发 Android 系列 13] 应用打包部署 开始倒叙咯................ 先更新大宝部署吧,这个章节比较的Easy,童鞋们不用费脑筋.点解?从界面上填写几个参 ... 
- 用Maven打包成EAR远程部署JBoss(二)——部署到远程JBoss
		用Maven打包成EAR远程部署JBoss(一)讲了如何使用Maven打包,可是在文章的最后也留下了一个问题,那就是如何将包部署到远程的JBoss中呢?近期在对之前的学习进行总结,发现少了这样一篇重要 ... 
- Linux 系统化学习系列文章总目录(持续更新中)
		本页内容都是本人系统化学习Linux 时整理出来的.这些文章中,绝大多数命令类内容都是翻译.整理man或info文档总结出来的,所以相对都比较完整. 本人的写作方式.风格也可能会让朋友一看就恶心到直接 ... 
- maven打包忽略静态资源解决办法,dispatchServlet拦截静态资源请求的解决办法
		问题: maven 打包时,有的文件打不进去target 解决: 因为maven打包默认打Java文件.在项目中的pom文件中加build标签 <build> <resources& ... 
- 一步步实现windows版ijkplayer系列文章之四——windows下编译ijkplyer版ffmpeg
		一步步实现windows版ijkplayer系列文章之一--Windows10平台编译ffmpeg 4.0.2,生成ffplay 一步步实现windows版ijkplayer系列文章之二--Ijkpl ... 
- Maven学习笔记(十二)-maven打包之resource配置
		版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u011781521/article/details/79052725 一.简介 构建Maven项目的 ... 
随机推荐
- KingbaseES 数据库Windows环境下注册失败分析
			关键字: KingbaseES.Java.Register.服务注册 一.安装前准备 1.1 软件环境要求 金仓数据库管理系统KingbaseES V8.0支持微软Windows 7.Windows ... 
- KingbaseES 数据库删除功能组件
			关键字: KingbaseES.卸载.删除功能 一.安装后检查 在安装完成后,可以通过以下几种方式进行安装正确性验证: 1. 查看安装日志,确认没有错误记录; 2. 查看开始菜单: 查看应用程 ... 
- 使用 Spring Boot Admin 监控应用状态
			程序员优雅哥 SpringBoot 2.7 实战基础 - 11 - 使用 Spring Boot Admin 监控应用状态 1 Spring Boot Actuator Spring Boot Act ... 
- 从零打造“乞丐版” React(一)——从命令式编程到声明式编程
			这个系列的目的是通过使用 JS 实现"乞丐版"的 React,让读者了解 React 的基本工作原理,体会 React 带来的构建应用的优势 1 HTML 构建静态页面 使用 HT ... 
- 3.Ceph 基础篇 - RBD 块存储使用
			文章转载自:https://mp.weixin.qq.com/s?__biz=MzI1MDgwNzQ1MQ==&mid=2247485253&idx=1&sn=24d9b06a ... 
- 使用容器运行的minio配置https(TLS)访问
			使用certgen生成证书 下载地址:https://github.com/minio/certgen/releases/tag/v0.0.2 下载地址:https://files.cnblogs.c ... 
- Security Context
			概述 Security Context(安全上下文)用来限制容器对宿主节点的可访问范围,以避免容器非法操作宿主节点的系统级别的内容,使得节点的系统或者节点上其他容器组受到影响. Security Co ... 
- HM VNISEdit2.0.3修正版
			HM VNISEdit,曾经是NSIS最强最佳开源免费编辑器/IDE,但2003年至今原作者已经接近20年未再更新,随着NSIS3.X版本的普及,NIS Edit不可避免的出现了大大小小的各种BUG, ... 
- vulnhub靶场之JANGOW: 1.0.1
			准备: 攻击机:虚拟机kali.本机win10. 靶机:JANGOW: 1.0.1,地址我这里设置的桥接,,下载地址:https://download.vulnhub.com/jangow/jango ... 
- 可观测性的常见用例|Techtarget
			[ 文章来源 ]https://www.techtarget.com/searchitoperations/tip/Common-use-cases-for-observability 这些可观测性用 ... 
