Skip to content

Commit

Permalink
support pom package manager
Browse files Browse the repository at this point in the history
  • Loading branch information
yndu13 committed Jun 7, 2024
1 parent 93d27c8 commit b82fa19
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
27 changes: 26 additions & 1 deletion lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class Visitor {
this.baseClient = option && option.baseClient || javaPackage + '.BaseClient';
this.package = javaPackage;
this.packageInfo = option.java.packageInfo;
this.packageManager = option.java.packageManager;
this.config = Object.assign({
outputDir: '',
indent: ' ',
Expand Down Expand Up @@ -270,7 +271,31 @@ class Visitor {
});
}
const builder = new xml2js.Builder();
let newPom = builder.buildObject(json);
pomFile = builder.buildObject(json);
const newJson = await parse(pomFile);
needAddDependencies = [];
Object.keys(this.packageManager || {}).forEach((value) => {
let needAdd = true;
let dependency = value.split(':');
var dependencyObject = {};
dependencyObject.groupId = dependency[0];
dependencyObject.artifactId = dependency[1];
dependencyObject.version = this.packageManager[`${dependency[0]}:${dependency[1]}`];
newJson.project.dependencies[0].dependency.forEach((dependency) => {
if (dependency.groupId[0] === dependencyObject.groupId
&& dependency.artifactId[0] === dependencyObject.artifactId) {
dependency.version[0] = dependencyObject.version;
needAdd = false;
}
});
if (needAdd) {
needAddDependencies.push(dependencyObject);
}
});
needAddDependencies.forEach((value) => {
newJson.project.dependencies[0].dependency.push(value);
});
let newPom = builder.buildObject(newJson);
if (this.exec) {
let mainClassPath = this.package + '.' + (this.className || 'Client');
newPom = newPom.split('${mainClass}').join(mainClassPath);
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/pom/Darafile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
"artifactId": "test",
"version": "1.0.0"
},
"packageManager": {
"com.aliyun:tea": "1.2.7",
"com.aliyun:something-need-to-add": "[0.0.1, 1.0.0)"
},
"typedef": {
"HttpResponse": {
"import": "okhttp3.Response",
Expand Down
7 changes: 6 additions & 1 deletion test/fixtures/pom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>tea</artifactId>
<version>1.1.14</version>
<version>1.2.7</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>something-need-to-add</artifactId>
<version>[0.0.1, 1.0.0)</version>
</dependency>
</dependencies>
<build>
Expand Down

0 comments on commit b82fa19

Please sign in to comment.