pax_global_header 0000666 0000000 0000000 00000000064 13756453243 0014526 g ustar 00root root 0000000 0000000 52 comment=23a6b7f7582284d93f7ca864b6172112b685c435
mvnparser-1.4.0/ 0000775 0000000 0000000 00000000000 13756453243 0013545 5 ustar 00root root 0000000 0000000 mvnparser-1.4.0/.github/ 0000775 0000000 0000000 00000000000 13756453243 0015105 5 ustar 00root root 0000000 0000000 mvnparser-1.4.0/.github/workflows/ 0000775 0000000 0000000 00000000000 13756453243 0017142 5 ustar 00root root 0000000 0000000 mvnparser-1.4.0/.github/workflows/ci.yml 0000664 0000000 0000000 00000001742 13756453243 0020264 0 ustar 00root root 0000000 0000000 name: Continuous Integration
on:
push:
branches:
- master
pull_request:
branches:
- '**'
jobs:
test:
strategy:
matrix:
go-version: [ 1.14.x, 1.15.x ]
platform: [ ubuntu-latest ]
name: ${{ matrix.platform }} @ Go ${{ matrix.go-version }}
runs-on: ${{ matrix.platform }}
steps:
- name: Install Go ${{ matrix.go-version }}
uses: actions/setup-go@v2.1.3
with:
go-version: ${{ matrix.go-version }}
- name: Set environment
run: |
echo "GOPATH=$(dirname $GITHUB_WORKSPACE)" >> $GITHUB_ENV
echo "$(dirname $GITHUB_WORKSPACE)/bin" >> $GITHUB_PATH
shell: bash
- name: Checkout code
uses: actions/checkout@v2
- name: Test
run: |
go test -v ./...
- name: Update go report card
if: success() && matrix.platform == 'ubuntu-latest'
continue-on-error: true
uses: creekorful/goreportcard-action@v1.0 mvnparser-1.4.0/.gitignore 0000664 0000000 0000000 00000000337 13756453243 0015540 0 ustar 00root root 0000000 0000000 # Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, build with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Jetbrains idea folder
.idea/ mvnparser-1.4.0/LICENSE 0000664 0000000 0000000 00000002056 13756453243 0014555 0 ustar 00root root 0000000 0000000 MIT License
Copyright (c) 2019 Aloïs Micard
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
mvnparser-1.4.0/README.md 0000664 0000000 0000000 00000005127 13756453243 0015031 0 ustar 00root root 0000000 0000000 # mvnparser
[](https://travis-ci.org/creekorful/mvnparser)
[](https://goreportcard.com/report/github.com/creekorful/mvnparser)
[](https://codeclimate.com/github/creekorful/mvnparser/maintainability)
Go parser for maven Project Object Model (POM) file
# how to use it ?
Let's take the following POM file
```xml
4.0.0
com.example
my-app
1.0.0-SNAPSHOT
junit
junit
test
javax.enterprise
cdi-api
provided
org.apache.maven.plugins
maven-compiler-plugin
3.8.0
11
```
You can read the pom file using
```go
package main
import (
"github.com/creekorful/mvnparser"
"encoding/xml"
"log"
)
func main() {
// filled with previously declared xml
pomStr := "..."
// Load project from string
var project mvnparser.MavenProject
if err := xml.Unmarshal([]byte(pomStr), &project); err != nil {
log.Fatalf("unable to unmarshal pom file. Reason: %s", err)
}
log.Print(project.GroupId) // -> com.example
log.Print(project.ArtifactId) // -> my-app
log.Print(project.Version) // -> 1.0.0-SNAPSHOT
// iterate over dependencies
for _, dep := range project.Dependencies {
log.Print(dep.GroupId)
log.Print(dep.ArtifactId)
log.Print(dep.Version)
// ...
}
}
``` mvnparser-1.4.0/go.mod 0000664 0000000 0000000 00000000060 13756453243 0014647 0 ustar 00root root 0000000 0000000 module github.com/creekorful/mvnparser
go 1.14
mvnparser-1.4.0/parser.go 0000664 0000000 0000000 00000010306 13756453243 0015370 0 ustar 00root root 0000000 0000000 // MIT License
//
// Copyright (c) 2019 Aloïs Micard
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package mvnparser
import (
"encoding/xml"
"io"
)
// Represent a POM file
type MavenProject struct {
XMLName xml.Name `xml:"project"`
ModelVersion string `xml:"modelVersion"`
Parent Parent `xml:"parent"`
GroupId string `xml:"groupId"`
ArtifactId string `xml:"artifactId"`
Version string `xml:"version"`
Packaging string `xml:"packaging"`
Name string `xml:"name"`
Repositories []Repository `xml:"repositories>repository"`
Properties Properties `xml:"properties"`
DependencyManagement DependencyManagement `xml:"dependencyManagement"`
Dependencies []Dependency `xml:"dependencies>dependency"`
Profiles []Profile `xml:"profiles"`
Build Build `xml:"build"`
PluginRepositories []PluginRepository `xml:"pluginRepositories>pluginRepository"`
Modules []string `xml:"modules>module"`
}
// Represent the parent of the project
type Parent struct {
GroupId string `xml:"groupId"`
ArtifactId string `xml:"artifactId"`
Version string `xml:"version"`
}
// Represent a dependency of the project
type Dependency struct {
XMLName xml.Name `xml:"dependency"`
GroupId string `xml:"groupId"`
ArtifactId string `xml:"artifactId"`
Version string `xml:"version"`
Classifier string `xml:"classifier"`
Type string `xml:"type"`
Scope string `xml:"scope"`
Exclusions []Exclusion `xml:"exclusions>exclusion"`
}
// Represent an exclusion
type Exclusion struct {
XMLName xml.Name `xml:"exclusion"`
GroupId string `xml:"groupId"`
ArtifactId string `xml:"artifactId"`
}
type DependencyManagement struct {
Dependencies []Dependency `xml:"dependencies>dependency"`
}
// Represent a repository
type Repository struct {
Id string `xml:"id"`
Name string `xml:"name"`
Url string `xml:"url"`
}
type Profile struct {
Id string `xml:"id"`
Build Build `xml:"build"`
}
type Build struct {
// todo: final name ?
Plugins []Plugin `xml:"plugins>plugin"`
}
type Plugin struct {
XMLName xml.Name `xml:"plugin"`
GroupId string `xml:"groupId"`
ArtifactId string `xml:"artifactId"`
Version string `xml:"version"`
//todo something like: Configuration map[string]string `xml:"configuration"`
// todo executions
}
// Represent a pluginRepository
type PluginRepository struct {
Id string `xml:"id"`
Name string `xml:"name"`
Url string `xml:"url"`
}
// Represent Properties
type Properties map[string]string
func (p *Properties) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
*p = map[string]string{}
for {
key := ""
value := ""
token, err := d.Token()
if err == io.EOF {
break
}
switch tokenType := token.(type) {
case xml.StartElement:
key = tokenType.Name.Local
err := d.DecodeElement(&value, &start)
if err != nil {
return err
}
(*p)[key] = value
}
}
return nil
}
mvnparser-1.4.0/parser_test.go 0000664 0000000 0000000 00000032067 13756453243 0016437 0 ustar 00root root 0000000 0000000 // MIT License
//
// Copyright (c) 2019 Aloïs Micard
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package mvnparser
import (
"encoding/xml"
"testing"
)
func TestUnmarshal(t *testing.T) {
pomStr := `
4.0.0
com.example
my-app
1.0.0-SNAPSHOT
jar
My App
fr.creekorful
parent-project
1.0.0-SNAPSHOT
core
dao
private-repository
My private repository
http://localhost:8081/repository/maven-private/
private-plugin-repository
My private plugin repository
http://localhost:8081/repository/maven-private/
UTF-8
SNAPSHOT
org.wildfly.bom
wildfly-javaee8-with-tools
${version.wildfly.bom}
pom
import
junit
junit
test
org.slf4j
slf4j-api
1.7.22
provided
javax.enterprise
cdi-api
provided
javax.persistence
javax.persistence-api
provided
org.hibernate
hibernate-core
provided
mysql
mysql-connector-java
8.0.15
provided
io.swagger.core.v3
swagger-jaxrs2
2.0.8
com.fasterxml.jackson.core
jackson-databind
com.fasterxml.jackson.jaxrs
jackson-jaxrs-json-provider
com.fasterxml.jackson.core
jackson-annotations
com.example
test-framework
1.0.0
jee8
test-jar
test
dev
org.wildfly.plugins
wildfly-maven-plugin
deploy_jdbc_driver
install
deploy-artifact
mysql
mysql-connector-java
mysql
mysql
${project.artifactId}-${build.number}
org.apache.maven.plugins
maven-compiler-plugin
3.8.0
11
maven-war-plugin
3.2.2
false
org.wildfly.plugins
wildfly-maven-plugin
2.0.1.Final
${project.artifactId}.war
org.jacoco
jacoco-maven-plugin
0.8.2
prepare-agent
`
var project MavenProject
if err := xml.Unmarshal([]byte(pomStr), &project); err != nil {
t.Errorf("unable to unmarshal pom file. Reason: %s", err)
}
// Validate fields
if project.GroupId != "com.example" {
t.Errorf("groupId does not match (expected: com.example, found: %s)", project.GroupId)
}
if project.ArtifactId != "my-app" {
t.Errorf("artifactId does not match (expected: my-app, found: %s)", project.ArtifactId)
}
if project.Version != "1.0.0-SNAPSHOT" {
t.Errorf("version does not match (expected: 1.0.0-SNAPSHOT, found: %s)", project.Version)
}
if project.Name != "My App" {
t.Errorf("artifactId does not match (expected: My App, found: %s)", project.Name)
}
if len(project.Repositories) != 1 {
t.Errorf("expecting 1 repository found %d", len(project.Repositories))
}
if project.Repositories[0].Id != "private-repository" {
t.Errorf("repository[0] id does not match (expected: private-repository, found: %s)", project.Repositories[0].Id)
}
if project.Repositories[0].Name != "My private repository" {
t.Errorf("repository[0] name does not match (expected: My private repository, found: %s)", project.Repositories[0].Name)
}
if project.Repositories[0].Url != "http://localhost:8081/repository/maven-private/" {
t.Errorf("repository[0] url does not match (expected: http://localhost:8081/repository/maven-private/, found: %s)", project.Repositories[0].Url)
}
// todo test properties
if len(project.DependencyManagement.Dependencies) != 1 {
t.Errorf("expecting 1 dependencies in management found %d", len(project.DependencyManagement.Dependencies))
}
if project.DependencyManagement.Dependencies[0].Type != "pom" {
t.Errorf("artifactId does not match (expected: My App, found: %s)", project.Name)
}
if len(project.Dependencies) != 8 {
t.Errorf("expecting 8 dependencies found %d", len(project.Dependencies))
}
if project.PluginRepositories[0].Id != "private-plugin-repository" {
t.Errorf("pluginRepository[0] id does not match (expected: private-plugin-repository, found: %s)", project.PluginRepositories[0].Id)
}
if project.PluginRepositories[0].Name != "My private plugin repository" {
t.Errorf("pluginRepository[0] name does not match (expected: My private plugin repository, found: %s)", project.PluginRepositories[0].Name)
}
if project.PluginRepositories[0].Url != "http://localhost:8081/repository/maven-private/" {
t.Errorf("pluginRepository[0] url does not match (expected: http://localhost:8081/repository/maven-private/, found: %s)", project.PluginRepositories[0].Url)
}
if len(project.Modules) != 2 {
t.Error("Number of module doesn't match")
}
if !contains(project.Modules, "core") {
t.Error("Module core absent")
}
if !contains(project.Modules, "dao") {
t.Error("Module dao absent")
}
if project.Parent.ArtifactId != "parent-project" {
t.Error("Wrong parent artifactId")
}
if project.Parent.Version != "1.0.0-SNAPSHOT" {
t.Error("Wrong parent version")
}
if project.Parent.GroupId != "fr.creekorful" {
t.Error("Wrong parent groupId")
}
if len(project.Properties) != 2 {
t.Error("All Properties should be read")
}
if val, _ := project.Properties["project.build.sourceEncoding"]; val != "UTF-8" {
t.Error("Property project.build.sourceEncoding should exists")
}
if val, _ := project.Properties["build.number"]; val != "SNAPSHOT" {
t.Error("build.number")
}
}
func TestEmptyProperties(t *testing.T) {
pomStr := `
4.0.0
com.example
my-app
1.0.0-SNAPSHOT
jar
My App
fr.creekorful
parent-project
1.0.0-SNAPSHOT
`
var project MavenProject
if err := xml.Unmarshal([]byte(pomStr), &project); err != nil {
t.Errorf("unable to unmarshal pom file. Reason: %s", err)
}
if len(project.Properties) != 0 {
t.Error("property map should be empty")
}
}
func TestWithoutProperties(t *testing.T) {
pomStr := `
4.0.0
com.example
my-app
1.0.0-SNAPSHOT
jar
My App
fr.creekorful
parent-project
1.0.0-SNAPSHOT
`
var project MavenProject
if err := xml.Unmarshal([]byte(pomStr), &project); err != nil {
t.Errorf("unable to unmarshal pom file. Reason: %s", err)
}
if len(project.Properties) != 0 {
t.Error("property map should be empty")
}
}
func contains(haystack []string, needle string) bool {
for _, wheat := range haystack {
if wheat == needle {
return true
}
}
return false
}