Skip to content
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
3 changes: 1 addition & 2 deletions internal/cmd/fetcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,7 @@ func recreateSwiftPackageResolved(ctx context.Context, logger *slog.Logger, plug
return nil
}

// regenerateMavenDeps regenerates the pom.xml and Dockerfile's maven-deps
// stage from the plugin's buf.plugin.yaml.
// regenerateMavenDeps regenerates the pom.xml from the plugin's buf.plugin.yaml.
func regenerateMavenDeps(plugin createdPlugin) error {
versionDir := filepath.Join(plugin.pluginDir, plugin.newVersion)
pluginsDir := filepath.Dir(filepath.Dir(plugin.pluginDir))
Expand Down
37 changes: 0 additions & 37 deletions internal/cmd/regenerate-maven-poms/main.go

This file was deleted.

112 changes: 0 additions & 112 deletions internal/maven/dockerfile.go

This file was deleted.

45 changes: 3 additions & 42 deletions internal/maven/regenerate.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package maven

import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/bufbuild/buf/private/bufpkg/bufremoteplugin/bufremotepluginconfig"
)

// RegenerateMavenDeps processes a Maven plugin version directory by
// merging transitive deps, deduplicating, rendering POM to a pom.xml
// file, and ensuring the Dockerfile has an up-to-date maven-deps
// stage. Returns nil without changes if the plugin has no Maven
// registry config.
// merging transitive deps, deduplicating, and rendering POM to a
// pom.xml file. Returns nil without changes if the plugin has no
// Maven registry config.
func RegenerateMavenDeps(pluginVersionDir, pluginsDir string) error {
yamlPath := filepath.Join(pluginVersionDir, "buf.plugin.yaml")
pluginConfig, err := bufremotepluginconfig.ParseConfig(yamlPath)
Expand All @@ -38,41 +35,5 @@ func RegenerateMavenDeps(pluginVersionDir, pluginsDir string) error {
if err := os.WriteFile(pomPath, []byte(pom), 0644); err != nil { //nolint:gosec
return fmt.Errorf("writing pom.xml: %w", err)
}
dockerfilePath := filepath.Join(pluginVersionDir, "Dockerfile")
dockerfileBytes, err := os.ReadFile(dockerfilePath)
if err != nil {
return err
}
updated, err := EnsureMavenDepsStage(string(dockerfileBytes))
if err != nil {
return fmt.Errorf("ensuring maven-deps stage: %w", err)
}
if err := os.WriteFile(dockerfilePath, []byte(updated), 0644); err != nil { //nolint:gosec
return fmt.Errorf("writing Dockerfile: %w", err)
}
dockerignorePath := filepath.Join(pluginVersionDir, ".dockerignore")
if err := ensureDockerignoreAllowsPOM(dockerignorePath); err != nil {
return fmt.Errorf("updating .dockerignore: %w", err)
}
return nil
}

// ensureDockerignoreAllowsPOM adds "!pom.xml" to the .dockerignore if it
// exists and doesn't already allow pom.xml. The pom.xml must be present in
// the build context for the maven-deps COPY instruction to succeed.
func ensureDockerignoreAllowsPOM(path string) error {
content, err := os.ReadFile(path)
if errors.Is(err, os.ErrNotExist) {
return nil
} else if err != nil {
return err
}
const pomRule = "!pom.xml"
for line := range strings.SplitSeq(string(content), "\n") {
if strings.TrimSpace(line) == pomRule {
return nil
}
}
updated := strings.TrimRight(string(content), "\n") + "\n" + pomRule + "\n"
return os.WriteFile(path, []byte(updated), 0644) //nolint:gosec
}
18 changes: 1 addition & 17 deletions internal/maven/regenerate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ registry:
require.NoError(t, os.WriteFile(filepath.Join(baseDir, "buf.plugin.yaml"), []byte(baseYAML), 0644))

// Setup: consumer-plugin depends on base-plugin and has its own Maven
// deps plus a lite runtime. The Dockerfile has a maven-deps stage.
// deps plus a lite runtime.
consumerDir := filepath.Join(tmpDir, "plugins", "test", "consumer-plugin", "v1.0.0")
require.NoError(t, os.MkdirAll(consumerDir, 0755))
consumerYAML := `version: v1
Expand All @@ -61,27 +61,11 @@ registry:
`
require.NoError(t, os.WriteFile(filepath.Join(consumerDir, "buf.plugin.yaml"), []byte(consumerYAML), 0644))

dockerfile := `# syntax=docker/dockerfile:1.19
FROM debian:bookworm AS build
RUN echo hello

FROM scratch
COPY --from=build /app .
ENTRYPOINT ["/app"]
`
require.NoError(t, os.WriteFile(filepath.Join(consumerDir, "Dockerfile"), []byte(dockerfile), 0644))

// Run RegenerateMavenDeps on the consumer plugin.
pluginsDir := filepath.Join(tmpDir, "plugins")
err := RegenerateMavenDeps(consumerDir, pluginsDir)
require.NoError(t, err)

// Verify the maven-deps stage was inserted into the Dockerfile.
dockerfileBytes, err := os.ReadFile(filepath.Join(consumerDir, "Dockerfile"))
require.NoError(t, err)
assert.Contains(t, string(dockerfileBytes), "FROM "+MavenImage+" AS maven-deps")
assert.Contains(t, string(dockerfileBytes), "COPY --from=maven-deps /root/.m2/repository /maven-repository")

// Read and parse pom.xml to verify deps include versions.
pomBytes, err := os.ReadFile(filepath.Join(consumerDir, "pom.xml"))
require.NoError(t, err)
Expand Down
Loading