support to generate project from maven archetype
This commit is contained in:
Родитель
e2bb8025fa
Коммит
ecb44f8efb
|
@ -3,7 +3,11 @@ All notable changes to the "vscode-maven" extension will be documented in this f
|
||||||
|
|
||||||
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
|
Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.
|
||||||
|
|
||||||
## [Unreleased]
|
## Unreleased
|
||||||
|
- Add context menu on `pom.xml`.
|
||||||
|
- Support maven archetype generate.
|
||||||
|
|
||||||
|
## Released
|
||||||
|
|
||||||
### 0.0.6
|
### 0.0.6
|
||||||
- Run command in dedicated terminals for each maven project.
|
- Run command in dedicated terminals for each maven project.
|
||||||
|
|
17
package.json
17
package.json
|
@ -98,6 +98,16 @@
|
||||||
"command": "mavenProject.openPom",
|
"command": "mavenProject.openPom",
|
||||||
"title": "Open POM file",
|
"title": "Open POM file",
|
||||||
"category": "Maven"
|
"category": "Maven"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"command": "mavenArchetype.generate",
|
||||||
|
"title": "Generate from Maven Archetype",
|
||||||
|
"category": "Maven"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"command": "mavenArchetype.updateCache",
|
||||||
|
"title": "Update Maven Archetype Catalog",
|
||||||
|
"category": "Maven"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"views": {
|
"views": {
|
||||||
|
@ -111,7 +121,10 @@
|
||||||
"menus": {
|
"menus": {
|
||||||
"explorer/context": [
|
"explorer/context": [
|
||||||
{
|
{
|
||||||
"command": "mavenArchetype.generate"
|
"command": "mavenArchetype.generate"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"command": "mavenArchetype.updateCache"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"command": "mavenProject.effectivePom",
|
"command": "mavenProject.effectivePom",
|
||||||
|
@ -225,4 +238,4 @@
|
||||||
"readdirp": "^2.1.0",
|
"readdirp": "^2.1.0",
|
||||||
"xml2js": "^0.4.19"
|
"xml2js": "^0.4.19"
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -87,6 +87,27 @@ export function activate(context: vscode.ExtensionContext) {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let commandMavenArchetypeGenerate = vscode.commands.registerCommand("mavenArchetype.generate", () => {
|
||||||
|
vscode.window.showWorkspaceFolderPick().then(ret => { console.log(ret); });
|
||||||
|
const archetypeList = Utils.getArchetypeList();
|
||||||
|
vscode.window.showQuickPick(archetypeList, { matchOnDescription: true }).then(selected => {
|
||||||
|
if (selected) {
|
||||||
|
const { artifactId, groupId } = selected;
|
||||||
|
vscode.window.showQuickPick(selected.versions).then(version => {
|
||||||
|
if (version) {
|
||||||
|
Utils.runInTerminal(`mvn archetype:generate -DarchetypeArtifactId=${artifactId} -DarchetypeGroupId=${groupId} -DarchetypeVersion=${version}`, true, 'Maven');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
let commandMavenArchetypeUpdateCache = vscode.commands.registerCommand("mavenArchetype.updateCache", () => {
|
||||||
|
vscode.window.showInputBox({ value: "http://repo.maven.apache.org/maven2/archetype-catalog.xml" }).then(url => {
|
||||||
|
vscode.window.setStatusBarMessage("Updating archetype catalog ... ", Utils.updateArchetypeCache(url));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
['clean', 'validate', 'compile', 'test', 'package', 'verify', 'install', 'site', 'deploy'].forEach(goal => {
|
['clean', 'validate', 'compile', 'test', 'package', 'verify', 'install', 'site', 'deploy'].forEach(goal => {
|
||||||
let commandMavenGoal = vscode.commands.registerCommand(`mavenGoal.${goal}`, (goalItem) => {
|
let commandMavenGoal = vscode.commands.registerCommand(`mavenGoal.${goal}`, (goalItem) => {
|
||||||
const item = goalItem as MavenProjectTreeItem;
|
const item = goalItem as MavenProjectTreeItem;
|
||||||
|
@ -99,6 +120,8 @@ export function activate(context: vscode.ExtensionContext) {
|
||||||
context.subscriptions.push(commandMavenGoalExecute);
|
context.subscriptions.push(commandMavenGoalExecute);
|
||||||
context.subscriptions.push(commandMavenProjectEffectivePom);
|
context.subscriptions.push(commandMavenProjectEffectivePom);
|
||||||
context.subscriptions.push(commandMavenProjectOpenPom);
|
context.subscriptions.push(commandMavenProjectOpenPom);
|
||||||
|
context.subscriptions.push(commandMavenArchetypeGenerate);
|
||||||
|
context.subscriptions.push(commandMavenArchetypeUpdateCache);
|
||||||
}
|
}
|
||||||
|
|
||||||
// this method is called when your extension is deactivated
|
// this method is called when your extension is deactivated
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
import * as vscode from "vscode";
|
||||||
|
import { QuickPickItem } from "vscode";
|
||||||
|
export class MavenArchetype implements QuickPickItem {
|
||||||
|
label: string;
|
||||||
|
description: string;
|
||||||
|
artifactId: string;
|
||||||
|
groupId: string;
|
||||||
|
versions: string[];
|
||||||
|
constructor(aid: string, gid: string, desc?: string) {
|
||||||
|
this.artifactId = aid;
|
||||||
|
this.groupId = gid;
|
||||||
|
this.versions = [];
|
||||||
|
this.label = `${gid}:${aid}`;
|
||||||
|
this.description = desc;
|
||||||
|
}
|
||||||
|
}
|
47
src/utils.ts
47
src/utils.ts
|
@ -1,11 +1,14 @@
|
||||||
import * as vscode from "vscode";
|
import * as vscode from "vscode";
|
||||||
import { execSync } from 'child_process';
|
import { execSync } from 'child_process';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
|
import * as http from 'http';
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
import * as md5 from "md5";
|
import * as md5 from "md5";
|
||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as xml2js from 'xml2js';
|
import * as xml2js from 'xml2js';
|
||||||
import { MavenProjectTreeItem } from "./mavenProjectTreeItem";
|
import { MavenProjectTreeItem } from "./mavenProjectTreeItem";
|
||||||
|
import { MavenArchetype } from "./mavenArchetype";
|
||||||
|
import { existsSync } from "fs";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -105,6 +108,50 @@ export class Utils {
|
||||||
return path.join(os.tmpdir(), "vscode-maven", md5(pomXmlFilePath), 'commandHistory.txt');
|
return path.join(os.tmpdir(), "vscode-maven", md5(pomXmlFilePath), 'commandHistory.txt');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static getArchetypeList(): MavenArchetype[] {
|
||||||
|
const localArchetypeXmlFilePath = this.getLocalArchetypeCatalogFilePath();
|
||||||
|
if (existsSync(localArchetypeXmlFilePath)) {
|
||||||
|
const xml = fs.readFileSync(localArchetypeXmlFilePath, 'utf8');
|
||||||
|
let catalog = null;
|
||||||
|
xml2js.parseString(xml, { explicitArray: false }, (err, res) => { catalog = res; });
|
||||||
|
if (catalog && catalog['archetype-catalog'] && catalog['archetype-catalog'].archetypes) {
|
||||||
|
let dict: { [key: string]: MavenArchetype } = {};
|
||||||
|
catalog['archetype-catalog'].archetypes.archetype.forEach(archetype => {
|
||||||
|
const identifier = `${archetype.groupId}:${archetype.artifactId}`;
|
||||||
|
if (!dict[identifier]) {
|
||||||
|
dict[identifier] = new MavenArchetype(archetype.artifactId, archetype.groupId, archetype.description);
|
||||||
|
}
|
||||||
|
if (dict[identifier].versions.indexOf(archetype.version) < 0) {
|
||||||
|
dict[identifier].versions.push(archetype.version);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return Object.keys(dict).map(k => dict[k]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
public static getLocalArchetypeCatalogFilePath(): string {
|
||||||
|
return path.join(os.homedir(), ".m2", "repository", "archetype-catalog.xml");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static updateArchetypeCache(url: string): Promise<void> {
|
||||||
|
const filepath = this.getLocalArchetypeCatalogFilePath();
|
||||||
|
this.mkdirp(path.dirname(filepath));
|
||||||
|
const file = fs.createWriteStream(filepath);
|
||||||
|
let ret = new Promise<void>((resolve, reject) => {
|
||||||
|
const request = http.get(url, (response) => {
|
||||||
|
response.pipe(file);
|
||||||
|
response.on("end", () => {
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
request.on("error", e => {
|
||||||
|
reject();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
private static mkdirp(filepath) {
|
private static mkdirp(filepath) {
|
||||||
if (fs.existsSync(filepath)) {
|
if (fs.existsSync(filepath)) {
|
||||||
return;
|
return;
|
||||||
|
|
Загрузка…
Ссылка в новой задаче