maven-dependencies 插件的作用就是从本地的 maven 仓库中提取 jar 包, 放到某个文件夹下面. 这个功能其实是很简单的.
我在一家银行工作时, 公司电脑都无法连外网, 所以无法通过 maven 下载 jar 包. 但是在公司电脑上开发时, 我又想使用 maven 进行编译, 打包等操作. 如果把我电脑上的 maven 仓库复制上去, 太大, 我想根据 pom.xml 只复制那些项目实际用到的 jar 包, 形成 maven 仓库.
首先需要进行如下配置
- targetDir=jars
- #always use / ranther than \pom=C:/Users/weidiao/Desktop/pabqa/pom.xml
- m2=C:/Users/weidiao/.m2
- #should put all jars together ?
- simple=true
targetDir 表示从本地 maven 仓库中复制到哪里去, pom 表示 pom.xml 的路径, simple 表示是否保留 maven 的目录结构. 如果 simple=true, 则不保留目录结构, 只复制 jar 包; 如果 simple=false, 则遵循 maven 仓库的目录格式.
下面的代码根据 pom.xml 从本地的 maven 仓库中复制信息到一个新的文件夹
- import com.alibaba.fastjson.JSON;
- import org.jsoup.Jsoup;
- import org.jsoup.nodes.Document;
- import org.jsoup.nodes.Element;
- import org.jsoup.select.Elements;
- import org.xml.sax.SAXException;
- import javax.xml.parsers.ParserConfigurationException;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.nio.file.StandardCopyOption;
- import java.util.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- /**
- * 给定本地 maven 仓库
- * pom.xml 文件
- */
- public class MavenJarExtractor {
- static class Dependency {
- String artifactId;
- String groupId;
- String version;
- public String getArtifactId() {
- return artifactId;
- }
- public void setArtifactId(String artifactId) {
- this.artifactId = artifactId;
- }
- public String getGroupId() {
- return groupId;
- }
- public void setGroupId(String groupId) {
- this.groupId = groupId;
- }
- public String getVersion() {
- return version;
- }
- public void setVersion(String version) {
- this.version = version;
- }
- public Path getPath() {
- return Paths.get(getGroupId().replace('.', '/'))
- .resolve(Paths.get(getArtifactId()))
- .resolve(getVersion());
- }
- public String getFileName() {
- return getArtifactId() + "-" + getVersion();
- }
- }
- static class CopyTask {
- Path src;
- Path des;
- public Path getSrc() {
- return src;
- }
- public void setSrc(Path src) {
- this.src = src;
- }
- public Path getDes() {
- return des;
- }
- public void setDes(Path des) {
- this.des = des;
- }
- }
- String reFirst(String pattern, String s, int group) {
- Pattern p = Pattern.compile(pattern);
- Matcher matcher = p.matcher(s);
- boolean found = matcher.find();
- if (found) {
- return matcher.group(group);
- } else return null;
- }
- void createDir(Path p) throws IOException {
- p = p.toAbsolutePath();
- if (Files.notExists(p)) {
- if (Files.notExists(p.getParent()))
- createDir(p.getParent());
- Files.createDirectory(p);
- }
- }
- void copyFolder(Path src, Path des, boolean simple) {
- try {
- Files.list(src).forEach(x -> {
- if (simple && !x.getFileName().toString().endsWith(".jar"))
- return;
- try {
- Files.copy(x, des.resolve(x.getFileName()), StandardCopyOption.REPLACE_EXISTING);
- } catch (IOException e) {
- e.printStackTrace();
- }
- });
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- List<Dependency> parseDom(String pomPath) throws IOException {
- // 解析 pom = 解析属性 + 解析 dependency
- Document dom = Jsoup.parse(Paths.get(pomPath).toFile(), "utf8");
- Element p = dom.selectFirst("properties");
- Map<String, String> properties = new HashMap<>();
- if (p != null) {
- Elements ps = p.children();
- for (Element i : ps) {
- properties.put(i.tagName(), i.text());
- }
- }
- List<Dependency> dependencyList = new ArrayList<>();
- for (Element dep : dom.select("dependency")) {
- Dependency dependency = new Dependency();
- dependencyList.add(dependency);
- dependency.setArtifactId(dep.getElementsByTag("artifactId").text());
- dependency.setGroupId(dep.getElementsByTag("groupId").text());
- dependency.setVersion(dep.getElementsByTag("version").text());
- if (dependency.getVersion().matches("\\$\\{.+\\}")) {
- String version = reFirst("\\$\\{(.+)\\}", dependency.getVersion(), 1);
- dependency.setVersion(properties.get(version));
- }
- }
- return dependencyList;
- }
- List<CopyTask> buildTask(List<Dependency> dependencyList, String m2, String targetDir, boolean simple) {
- // 定义任务列表
- List<CopyTask> tasks = new ArrayList<>();
- for (Dependency i : dependencyList) {
- Path depDir = Paths.get(m2).resolve("repository").resolve(i.getPath());
- if (Files.notExists(depDir)) {
- throw new RuntimeException("没有在"+depDir+"找到" + i.getGroupId() + " " + i.getArtifactId());
- }
- CopyTask task = new CopyTask();
- task.setSrc(depDir);
- if (simple) {
- task.setDes(Paths.get(targetDir));
- } else {
- task.setDes(Paths.get(targetDir).resolve("repository").resolve(i.getPath()));
- }
- tasks.add(task);
- }
- System.out.println(JSON.toJSONString(tasks, true));
- return tasks;
- }
- void executeTask(List<CopyTask> tasks, boolean simple) throws IOException {
- // 执行任务
- for (CopyTask task : tasks) {
- if (Files.notExists(task.des)) {
- createDir(task.des);
- }
- copyFolder(task.getSrc(), task.getDes(), simple);
- }
- System.out.println("task over successfully");
- }
- MavenJarExtractor(String targetDir, String pom, String m2, boolean simple) throws IOException {
- List<Dependency> dependencies = parseDom(pom);
- List<CopyTask> tasks = buildTask(dependencies, m2, targetDir, simple);
- executeTask(tasks, simple);
- }
- public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
- // 加载配置
- Properties config = new Properties();
- config.load(new InputStreamReader(new FileInputStream("mavenjar.properties")));
- String targetDir = config.getProperty("targetDir", "target");
- String m2 = config.getProperty("m2", Paths.get(System.getProperty("user.home")).resolve(".m2").toString());
- String pomPath = config.getProperty("pom");//"C:\\Users\\weidiao\\Desktop\\pabqa\\pom.xml";
- boolean simple = Boolean.parseBoolean(config.getProperty("simple"));
- MavenJarExtractor extractor = new MavenJarExtractor(targetDir, pomPath, m2, simple);
- }
- }
需要依赖的 jar 包如下所示:
- xml version="1.0" encoding="UTF-8"?>
- 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">
- 4.0.0
- wyf
- mavenjar
- 1.0-SNAPSHOT
- UTF-8
- 1.8
- 1.8
- org.jsoup
- jsoup
- 1.11.2
- com.alibaba
- fastjson
- 1.2.44
- org.apache.maven.plugins
- maven-jar-plugin
- 2.6
- true
- }2${/outputDirectory>
来源: http://www.bubuko.com/infodetail-3107304.html