-
Notifications
You must be signed in to change notification settings - Fork 214
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
Use effective pom to analyze pom.xml #4710
Changes from 6 commits
71f2315
5fb823a
64a8bb2
8c64955
790db5a
43d2aea
b789faf
82726b3
44f0937
f9d8d0c
c3fefbe
08b35dd
cbcd1df
90e806f
bfab5a3
8050e92
0db693a
8ee2edf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package appdetect | ||
|
||
import ( | ||
"context" | ||
"maps" | ||
"slices" | ||
"strings" | ||
|
||
"github.com/azure/azure-dev/cli/azd/pkg/tools/maven" | ||
) | ||
|
||
type mavenProject struct { | ||
pom pom | ||
} | ||
|
||
func toMavenProject(ctx context.Context, mvnCli *maven.Cli, pomFilePath string) (mavenProject, error) { | ||
pom, err := toPom(ctx, mvnCli, pomFilePath) | ||
if err != nil { | ||
return mavenProject{}, err | ||
} | ||
return mavenProject{pom: pom}, nil | ||
} | ||
|
||
func detectDependencies(mavenProject mavenProject, project *Project) (*Project, error) { | ||
databaseDepMap := map[DatabaseDep]struct{}{} | ||
for _, dep := range mavenProject.pom.Dependencies { | ||
if dep.GroupId == "com.mysql" && dep.ArtifactId == "mysql-connector-j" { | ||
databaseDepMap[DbMySql] = struct{}{} | ||
} | ||
|
||
if dep.GroupId == "org.postgresql" && dep.ArtifactId == "postgresql" { | ||
databaseDepMap[DbPostgres] = struct{}{} | ||
} | ||
} | ||
|
||
if len(databaseDepMap) > 0 { | ||
project.DatabaseDeps = slices.SortedFunc(maps.Keys(databaseDepMap), | ||
func(a, b DatabaseDep) int { | ||
return strings.Compare(string(a), string(b)) | ||
}) | ||
} | ||
|
||
return project, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package appdetect | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a general There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
import ( | ||
"context" | ||
"encoding/xml" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/azure/azure-dev/cli/azd/pkg/tools/maven" | ||
) | ||
|
||
func toPom(ctx context.Context, mvnCli *maven.Cli, pomFilePath string) (pom, error) { | ||
result, err := toEffectivePom(ctx, mvnCli, pomFilePath) | ||
if err == nil { | ||
result.path = filepath.Dir(pomFilePath) | ||
return result, nil | ||
} | ||
|
||
result, err = unmarshalPomFile(pomFilePath) | ||
if err == nil { | ||
result.path = filepath.Dir(pomFilePath) | ||
// todo: handle pom, for example: <version>${project.version}<version> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, @weikanglim
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previous comment here: #4473 (comment) I still strongly suggest we do not do this. A user is asking I don't like the idea of trying to replicate maven evaluation because evaluation rules can be tricky to replicate; and I prefer tools that just work exactly, instead of tools that are subtly wrong. This can lead to downstream impacts that are harder to debug. On the flip side, if we are confident in replicating maven evaluation rules, I would prefer removing the current effective-POM evaluation and have the native-go implementation take over -- it's cool work to have a 100% compatible maven evaluator in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we end up deciding removing the maven evaluation, perhaps There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, @weikanglim
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Agree. Now effective pom is generated by There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, I think it's better than doing a partial analysis There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated in this commit: bfab5a3 |
||
return result, nil | ||
} | ||
return pom{}, err | ||
} | ||
|
||
func toEffectivePom(ctx context.Context, mvnCli *maven.Cli, pomFilePath string) (pom, error) { | ||
effectivePom, err := mvnCli.EffectivePom(ctx, pomFilePath) | ||
if err != nil { | ||
return pom{}, err | ||
} | ||
var resultPom pom | ||
err = xml.Unmarshal([]byte(effectivePom), &resultPom) | ||
return resultPom, err | ||
} | ||
|
||
func unmarshalPomFile(pomFilePath string) (pom, error) { | ||
bytes, err := os.ReadFile(pomFilePath) | ||
if err != nil { | ||
return pom{}, err | ||
} | ||
|
||
var result pom | ||
if err := xml.Unmarshal(bytes, &result); err != nil { | ||
return pom{}, fmt.Errorf("parsing xml: %w", err) | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
// pom represents the top-level structure of a Maven POM file. | ||
type pom struct { | ||
XmlName xml.Name `xml:"project"` | ||
Parent parent `xml:"parent"` | ||
Modules []string `xml:"modules>module"` // Capture the modules | ||
Dependencies []dependency `xml:"dependencies>dependency"` | ||
DependencyManagement dependencyManagement `xml:"dependencyManagement"` | ||
Build build `xml:"build"` | ||
path string | ||
} | ||
|
||
// Parent represents the parent POM if this project is a module. | ||
type parent struct { | ||
GroupId string `xml:"groupId"` | ||
ArtifactId string `xml:"artifactId"` | ||
Version string `xml:"version"` | ||
} | ||
|
||
// Dependency represents a single Maven dependency. | ||
type dependency struct { | ||
GroupId string `xml:"groupId"` | ||
ArtifactId string `xml:"artifactId"` | ||
Version string `xml:"version"` | ||
Scope string `xml:"scope,omitempty"` | ||
} | ||
|
||
// DependencyManagement includes a list of dependencies that are managed. | ||
type dependencyManagement struct { | ||
Dependencies []dependency `xml:"dependencies>dependency"` | ||
} | ||
|
||
// Build represents the build configuration which can contain plugins. | ||
type build struct { | ||
Plugins []plugin `xml:"plugins>plugin"` | ||
} | ||
|
||
// Plugin represents a build plugin. | ||
type plugin struct { | ||
GroupId string `xml:"groupId"` | ||
ArtifactId string `xml:"artifactId"` | ||
Version string `xml:"version"` | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see us adding more properties here... Are we okay keeping what we have to avoid code churn?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm OK to delete mavenProject and use pom directly.
Hi, @saragluna , This option is required by you before. Now if you agree, I'll delete mavenProject and use pom directly. I perfer
pom
instead ofmavenProject
because it's unmarshalled from pom.xmlThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you could go ahead please. But what I meant (insisted) is we should call this concept a maven project, instead of just a pom, which conceptually should be under the maven project.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make the PR been merged as soon as possible, I'll revert all refactor-related changes, just keep necessary changes.