Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add maven mergeMavenPomContent and sort build configuration #26530

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions generators/maven/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export default class MavenGenerator extends BaseApplicationGenerator<SpringBootG
}
};
}
source.mergeMavenPomContent = content => this.pomStorage.merge(content);
source.addMavenAnnotationProcessor = createForEach(artifact => this.pomStorage.addAnnotationProcessor(artifact));
source.addMavenDependency = createForEach(artifact => this.pomStorage.addDependency(artifact));
source.addMavenDependencyManagement = createForEach(artifact => this.pomStorage.addDependencyManagement(artifact));
Expand Down
10 changes: 5 additions & 5 deletions generators/maven/internal/xml-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ export default class XmlStorage {
this.persist(false);
}

public merge(source) {
assert(typeof source === 'object', 'Storage `merge` method only accept objects');
this._cachedStore = merge({}, this.store, source);
}

protected sort() {}

protected persist(sort = this.sortFile) {
Expand All @@ -107,11 +112,6 @@ export default class XmlStorage {
}
}

protected merge(source) {
assert(typeof source === 'object', 'Storage `merge` method only accept objects');
this._cachedStore = merge({}, this.store, source);
}

protected mergeContent<T>(existing: T, newContent?: string): T {
return newContent ? { ...existing, ...this.parser.parse(newContent) } : existing;
}
Expand Down
25 changes: 20 additions & 5 deletions generators/maven/support/pom-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ const formatFirstXmlLevel = content =>
'\n$1',
);

const sortSection = section => {
return Object.fromEntries(
Object.entries(section).sort(([key1, value1], [key2, value2]) => {
if (typeof value1 === typeof value2) key1.localeCompare(key2);
if (typeof value1 === 'string') return -1;
if (typeof value2 === 'string') return 1;
return 0;
}),
);
};

const isComment = name => name.startsWith('#');

const toMaxInt = nr => (nr === -1 ? Number.MAX_SAFE_INTEGER : nr);
Expand Down Expand Up @@ -334,11 +345,15 @@ export default class PomStorage extends XmlStorage {
if (Array.isArray(project.dependencyManagement?.dependencies?.dependency)) {
project.dependencyManagement.dependencies.dependency = sortArtifacts(project.dependencyManagement.dependencies.dependency);
}
if (Array.isArray(project.build?.plugins?.plugin)) {
project.build.plugins.plugin = sortArtifacts(project.build.plugins.plugin);
}
if (Array.isArray(project.build?.pluginManagement?.plugins?.plugin)) {
project.build.pluginManagement.plugins.plugin = sortArtifacts(project.build.pluginManagement.plugins.plugin);
if (project.build) {
project.build = sortSection(project.build);

if (Array.isArray(project.build.plugins?.plugin)) {
project.build.plugins.plugin = sortArtifacts(project.build.plugins.plugin);
}
if (Array.isArray(project.build.pluginManagement?.plugins?.plugin)) {
project.build.pluginManagement.plugins.plugin = sortArtifacts(project.build.pluginManagement.plugins.plugin);
}
}
if (Array.isArray(project.profiles?.profile)) {
project.profiles.profile = sortProfiles(project.profiles.profile);
Expand Down
1 change: 1 addition & 0 deletions generators/maven/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export type MavenDefinition = {
};

export type MavenSourceType = {
mergeMavenPomContent?(content: any): void;
addMavenAnnotationProcessor?(artifact: MavenAnnotationProcessor | MavenAnnotationProcessor[]): void;
addMavenDependency?(dependency: MavenDependency | MavenDependency[]): void;
addMavenDependencyManagement?(dependency: MavenDependency | MavenDependency[]): void;
Expand Down
Loading