Skip to content

Commit c5718f4

Browse files
authored
feat(internal/librarian/java): source google-cloud-pom-parent in pom.xml templates (#6432)
Update logic to parse `google-cloud-pom-parent` version separately from librarian.yaml, and update pom.xml templates to use dedicated marker comment for "google-cloud-pom-parent" to match versions.txt (googleapis/google-cloud-java#13483). Note: This logic is added in parallel to existing "monorepoVersion" logic. Will do a followup to validate both config in `Validate` step and simplify logic when accessing these. For #6411
1 parent dcb6e6c commit c5718f4

11 files changed

Lines changed: 54 additions & 17 deletions

File tree

‎internal/librarian/java/generate.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const (
3939
var (
4040
errNoProtos = errors.New("no protos found")
4141
errMonorepoVersion = fmt.Errorf("failed to find monorepo version for %q in config", rootLibrary)
42+
errParentVersion = fmt.Errorf("failed to find parent version for %q in config", parentPOM)
4243
errBOMVersionMissing = errors.New("libraries bom version not found in config")
4344
errUnrecognizedAPI = errors.New("unrecognized non-cloud API: configure java.group_id and java.distribution_name_override in librarian.yaml")
4445
// nonRecursivePaths is a set of paths where proto gathering should not be recursive.

‎internal/librarian/java/generate_test.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,7 @@ func TestGenerate_Logic(t *testing.T) {
677677
Libraries: []*config.Library{
678678
library,
679679
{Name: rootLibrary, Version: "1.2.3"},
680+
{Name: parentPOM, Version: "1.2.3"},
680681
},
681682
}
682683
// Setup mandatory files for postProcessAPI and syncPOMs
@@ -753,6 +754,7 @@ func TestGenerate_ProtoExclusion(t *testing.T) {
753754
Libraries: []*config.Library{
754755
library,
755756
{Name: rootLibrary, Version: "1.2.3"},
757+
{Name: parentPOM, Version: "1.2.3"},
756758
},
757759
}
758760
err := Generate(t.Context(), cfg, library, &sources.Sources{Googleapis: googleapisDir})

‎internal/librarian/java/pom.go‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ type bomParentPOMData struct {
7272
MainModule Coordinate
7373
Name string
7474
MonorepoVersion string
75+
ParentVersion string
7576
Modules []Coordinate
7677
}
7778

@@ -216,8 +217,8 @@ func discoverModules(library *config.Library, libraryDir string, transports map[
216217
// and parent POMs when new proto or gRPC modules are added. It returns a list
217218
// of newly created artifact version entries to be added to versions.txt.
218219
// TODO(https://github.com/googleapis/librarian/issues/5529): remove returning version entries.
219-
func syncPOMs(library *config.Library, libraryDir, monorepoVersion string, metadata *repoMetadata, transports map[string]serviceconfig.Transport) error {
220-
modules, err := collectModules(library, libraryDir, monorepoVersion, metadata, transports)
220+
func syncPOMs(library *config.Library, libraryDir, monorepoVersion, parentVersion string, metadata *repoMetadata, transports map[string]serviceconfig.Transport) error {
221+
modules, err := collectModules(library, libraryDir, monorepoVersion, parentVersion, metadata, transports)
221222
if err != nil {
222223
return err
223224
}
@@ -396,7 +397,7 @@ func detectIndentation(content string, index int) string {
396397
// All expected modules are collected (even if they exist) because the client
397398
// module's POM requires a full list of all proto and gRPC dependencies
398399
// to ensure its dependency list is fully synchronized.
399-
func collectModules(library *config.Library, libraryDir, monorepoVersion string, metadata *repoMetadata, transports map[string]serviceconfig.Transport) ([]javaModule, error) {
400+
func collectModules(library *config.Library, libraryDir, monorepoVersion, parentVersion string, metadata *repoMetadata, transports map[string]serviceconfig.Transport) ([]javaModule, error) {
400401
expectedModules, err := discoverModules(library, libraryDir, transports)
401402
if err != nil {
402403
return nil, err
@@ -456,6 +457,7 @@ func collectModules(library *config.Library, libraryDir, monorepoVersion string,
456457
MainModule: libCoord.GAPIC,
457458
Name: metadata.NamePretty,
458459
MonorepoVersion: monorepoVersion,
460+
ParentVersion: parentVersion,
459461
Modules: allModules,
460462
}
461463
template = bomPOMTemplateName
@@ -464,6 +466,7 @@ func collectModules(library *config.Library, libraryDir, monorepoVersion string,
464466
MainModule: libCoord.GAPIC,
465467
Name: metadata.NamePretty,
466468
MonorepoVersion: monorepoVersion,
469+
ParentVersion: parentVersion,
467470
Modules: allModules,
468471
}
469472
template = parentPOMTemplateName
@@ -522,3 +525,14 @@ func findMonorepoVersion(cfg *config.Config) (string, error) {
522525
}
523526
return "", errMonorepoVersion
524527
}
528+
529+
// TODO(https://github.com/googleapis/librarian/issues/6411):
530+
// Simplify logic here and check at validate step.
531+
func findParentPOMVersion(cfg *config.Config) (string, error) {
532+
for _, lib := range cfg.Libraries {
533+
if lib.Name == parentPOM {
534+
return lib.Version, nil
535+
}
536+
}
537+
return "", errParentVersion
538+
}

‎internal/librarian/java/pom_test.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func TestSyncPOMs_Golden(t *testing.T) {
7272
if err != nil {
7373
t.Fatal(err)
7474
}
75-
err = syncPOMs(library, tmpDir, "1.2.3", metadata, transports)
75+
err = syncPOMs(library, tmpDir, "1.2.3", "1.2.3", metadata, transports)
7676
if err != nil {
7777
t.Fatal(err)
7878
}
@@ -206,7 +206,7 @@ func TestSyncPOMs_Update(t *testing.T) {
206206
APIDescription: "Stores sensitive data such as API keys, passwords, and certificates.\nProvides convenience while improving security.",
207207
}
208208

209-
if err := syncPOMs(library, tmpDir, "1.2.3", metadata, transports); err != nil {
209+
if err := syncPOMs(library, tmpDir, "1.2.3", "1.2.3", metadata, transports); err != nil {
210210
t.Fatal(err)
211211
}
212212

@@ -272,7 +272,7 @@ func TestSyncPOMs_NoUpdate(t *testing.T) {
272272
APIDescription: "Stores sensitive data such as API keys, passwords, and certificates.\nProvides convenience while improving security.",
273273
}
274274

275-
if err := syncPOMs(library, tmpDir, "1.2.3", metadata, transports); err != nil {
275+
if err := syncPOMs(library, tmpDir, "1.2.3", "1.2.3", metadata, transports); err != nil {
276276
t.Fatal(err)
277277
}
278278

@@ -577,7 +577,7 @@ func TestCollectModules(t *testing.T) {
577577
if test.setup != nil {
578578
test.setup(t, tmpDir)
579579
}
580-
got, err := collectModules(test.library, tmpDir, test.monorepoVersion, test.metadata, test.transports)
580+
got, err := collectModules(test.library, tmpDir, test.monorepoVersion, test.monorepoVersion, test.metadata, test.transports)
581581
if err != nil {
582582
t.Fatal(err)
583583
}

‎internal/librarian/java/postgenerate.go‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ const (
3232
// rootLibrary is the name of the monorepo library used to identify
3333
// the version for all libraries in the repository.
3434
rootLibrary = "google-cloud-java"
35+
// parentPOM is the name of the parent POM library.
36+
parentPOM = "google-cloud-pom-parent"
3537
// gapicBOM is the name of the directory and artifact ID for the
3638
// generated Bill of Materials (BOM) for all GAPIC libraries.
3739
gapicBOM = "gapic-libraries-bom"
@@ -100,6 +102,13 @@ func PostGenerate(ctx context.Context, repoPath string, cfg *config.Config, miss
100102
if monorepoVersion == "" {
101103
return fmt.Errorf("%s library not found in librarian.yaml", rootLibrary)
102104
}
105+
parentVersion, err := findParentPOMVersion(cfg)
106+
if err != nil {
107+
return err
108+
}
109+
if parentVersion == "" {
110+
return fmt.Errorf("%s library not found in librarian.yaml", parentPOM)
111+
}
103112

104113
// TODO(https://github.com/googleapis/librarian/issues/5529): remove appending to versions.txt.
105114
versions := constructVersionLines(missingArtifacts)
@@ -118,7 +127,7 @@ func PostGenerate(ctx context.Context, repoPath string, cfg *config.Config, miss
118127
if err != nil {
119128
return fmt.Errorf("failed to search for BOM artifacts: %w", err)
120129
}
121-
if err := generateGAPICLibrariesBOM(repoPath, monorepoVersion, bomConfigs); err != nil {
130+
if err := generateGAPICLibrariesBOM(repoPath, monorepoVersion, parentVersion, bomConfigs); err != nil {
122131
return fmt.Errorf("failed to generate %s: %w", gapicBOM, err)
123132
}
124133
return nil
@@ -325,17 +334,19 @@ func generateRootPOM(repoPath string, modules []string) error {
325334

326335
// generateGAPICLibrariesBOM writes the gapic-libraries-bom/pom.xml file, which manages
327336
// versions for all individual library BOMs in the monorepo.
328-
func generateGAPICLibrariesBOM(repoPath, version string, bomConfigs []*bomConfig) error {
337+
func generateGAPICLibrariesBOM(repoPath, version, parentVersion string, bomConfigs []*bomConfig) error {
329338
bomDir := filepath.Join(repoPath, gapicBOM)
330339
if err := os.MkdirAll(bomDir, 0755); err != nil {
331340
return err
332341
}
333342
data := struct {
334-
Version string
335-
BOMConfigs []*bomConfig
343+
Version string
344+
ParentVersion string
345+
BOMConfigs []*bomConfig
336346
}{
337-
Version: version,
338-
BOMConfigs: bomConfigs,
347+
Version: version,
348+
ParentVersion: parentVersion,
349+
BOMConfigs: bomConfigs,
339350
}
340351
return writePOM(filepath.Join(bomDir, "pom.xml"), "gapic-libraries-bom.xml.tmpl", data)
341352
}

‎internal/librarian/java/postgenerate_test.go‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func TestPostGenerate(t *testing.T) {
4141
Language: "java",
4242
Libraries: []*config.Library{
4343
{Name: rootLibrary, Version: "1.2.3"},
44+
{Name: parentPOM, Version: "1.2.3"},
4445
{Name: "analytics-admin", Version: "0.98.0"},
4546
{Name: "area120-tables", Version: "0.92.0"},
4647
{Name: "aiplatform", Version: "3.89.0"},
@@ -196,6 +197,7 @@ func TestPostGenerate_SearchError(t *testing.T) {
196197
cfg := &config.Config{
197198
Libraries: []*config.Library{
198199
{Name: rootLibrary, Version: "1.2.3"},
200+
{Name: parentPOM, Version: "1.2.3"},
199201
},
200202
}
201203
err := PostGenerate(t.Context(), tmpDir, cfg, nil)
@@ -215,6 +217,7 @@ func TestPostGenerate_Error(t *testing.T) {
215217
cfg := &config.Config{
216218
Libraries: []*config.Library{
217219
{Name: rootLibrary, Version: "1.2.3"},
220+
{Name: parentPOM, Version: "1.2.3"},
218221
},
219222
}
220223
err := PostGenerate(t.Context(), tmpDir, cfg, nil)

‎internal/librarian/java/postprocess.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ func postProcessLibrary(ctx context.Context, params libraryPostProcessParams) er
8686
if err != nil {
8787
return err
8888
}
89-
if err := syncPOMs(params.library, params.outDir, monorepoVersion, params.metadata, params.transports); err != nil {
89+
parentVersion, err := findParentPOMVersion(params.cfg)
90+
if err != nil {
91+
return err
92+
}
93+
if err := syncPOMs(params.library, params.outDir, monorepoVersion, parentVersion, params.metadata, params.transports); err != nil {
9094
return fmt.Errorf("%w: %w", errSyncPOMs, err)
9195
}
9296

‎internal/librarian/java/postprocess_test.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ func TestPostProcessLibrary(t *testing.T) {
556556
defaultCfg := &config.Config{
557557
Libraries: []*config.Library{
558558
{Name: rootLibrary, Version: "1.0.0"},
559+
{Name: parentPOM, Version: "1.0.0"},
559560
},
560561
Default: &config.Default{
561562
Java: &config.JavaModule{
@@ -657,6 +658,7 @@ func TestPostProcessLibrary_ErrorCase(t *testing.T) {
657658
defaultCfg := &config.Config{
658659
Libraries: []*config.Library{
659660
{Name: rootLibrary, Version: "1.0.0"},
661+
{Name: parentPOM, Version: "1.0.0"},
660662
},
661663
Default: &config.Default{
662664
Java: &config.JavaModule{

‎internal/librarian/java/template/gapic-libraries-bom.xml.tmpl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<parent>
1616
<artifactId>google-cloud-pom-parent</artifactId>
1717
<groupId>com.google.cloud</groupId>
18-
<version>{{.Version}}</version><!-- {x-version-update:google-cloud-java:current} -->
18+
<version>{{.ParentVersion}}</version><!-- {x-version-update:google-cloud-pom-parent:current} -->
1919
<relativePath>../google-cloud-pom-parent/pom.xml</relativePath>
2020
</parent>
2121

‎internal/librarian/java/template/module_bom_pom.xml.tmpl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<parent>
99
<groupId>com.google.cloud</groupId>
1010
<artifactId>google-cloud-pom-parent</artifactId>
11-
<version>{{.MonorepoVersion}}</version><!-- {x-version-update:google-cloud-java:current} -->
11+
<version>{{.ParentVersion}}</version><!-- {x-version-update:google-cloud-pom-parent:current} -->
1212
<relativePath>../../google-cloud-pom-parent/pom.xml</relativePath>
1313
</parent>
1414

0 commit comments

Comments
 (0)