gopom

package module
v0.0.0-...-6d0738d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 4, 2024 License: MIT Imports: 4 Imported by: 2

README

gopom - a maven pom.xml parser

Tests Go Report Card

gopom is a Golang module to easily parse and work with maven pom.xml files.

Supports the offical pom.xml structure that can be read about here.

Modifications and why

This is forked from: github.com/2000Slash/gopom, but with the mod to round trip the Configuration correctly (for example Plugin.Configuration). Correctly here means it is not worrying about turning it into modifiable struct, but that the entire pom roundtrips correctly (parse, modify some bits, then marshal), should produce a diff that makes sense. With the upstream, it drops a bunch of stuff unless it's a strict key/value list. This version also handles ordering correctly in a few places.

Installation

go get -u github.com/chainguard-dev/gopom

Usage

Unmarshaling

To load and parse a pom.xml file it is possible to use the gopom.Parse(path string) function which will load the file at the given path and return the parsed pom. See below for example:

package main

import (
	"github.com/chainguard-dev/gopom"
	"log"
)

func main() {

	var pomPath string = ... // Path to the pom.xml file
	parsedPom, err := gopom.Parse(pomPath)
	if err != nil {
		log.Fatal(err)
	}
}

If one already has the pom.xml loaded as a string or bytes you can use encoding/xml from the standard library. This can be seen below:

package main

import (
	"encoding/xml"
	"github.com/chainguard-dev/gopom"
	"log"
)

func main() {
	var pomString string = ... // The pom string

	var parsedPom gopom.Project
	err := xml.Unmarshal([]byte(pomString), &parsedPom)
	if err != nil {
		log.Fatal(err)
	}
}
Marshaling

You can also marshal the project back to an xml, and for that you MUST use the Marshal function, because of the way things are named, and handled.

package main

import (
	"fmt"
	"log"

	"github.com/chainguard-dev/gopom"
)

func main() {

	var pomPath string = "./pom.xml"
	parsedPom, err := gopom.Parse(pomPath)
	if err != nil {
		log.Fatal(err)
	}
	output, _ := parsedPom.Marshal()
	fmt.Println(string(output))
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

Original work Copyright (c) 2020-present Viktor Franzén Modified work Copyright (c) 2021 Nils Hartmann

Licensed under MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Activation

type Activation struct {
	ActiveByDefault bool                `xml:"activeByDefault,omitempty"`
	JDK             string              `xml:"jdk,omitempty"`
	OS              *ActivationOS       `xml:"os,omitempty"`
	Property        *ActivationProperty `xml:"property,omitempty"`
	File            *ActivationFile     `xml:"file,omitempty"`
}

type ActivationFile

type ActivationFile struct {
	Missing string `xml:"missing,omitempty"`
	Exists  string `xml:"exists,omitempty"`
}

type ActivationOS

type ActivationOS struct {
	Name    string `xml:"name,omitempty"`
	Family  string `xml:"family,omitempty"`
	Arch    string `xml:"arch,omitempty"`
	Version string `xml:"version,omitempty"`
}

type ActivationProperty

type ActivationProperty struct {
	Name  string `xml:"name,omitempty"`
	Value string `xml:"value,omitempty"`
}

type Build

type Build struct {
	SourceDirectory       string       `xml:"sourceDirectory,omitempty"`
	ScriptSourceDirectory string       `xml:"scriptSourceDirectory,omitempty"`
	TestSourceDirectory   string       `xml:"testSourceDirectory,omitempty"`
	OutputDirectory       string       `xml:"outputDirectory,omitempty"`
	TestOutputDirectory   string       `xml:"testOutputDirectory,omitempty"`
	Extensions            *[]Extension `xml:"extensions>extension,omitempty"`
	BuildBase
}

type BuildBase

type BuildBase struct {
	DefaultGoal      string            `xml:"defaultGoal,omitempty"`
	Resources        *[]Resource       `xml:"resources>resource,omitempty"`
	TestResources    *[]Resource       `xml:"testResources>testResource,omitempty"`
	Directory        string            `xml:"directory,omitempty"`
	FinalName        string            `xml:"finalName,omitempty"`
	Filters          *[]string         `xml:"filters>filter,omitempty"`
	PluginManagement *PluginManagement `xml:"pluginManagement,omitempty"`
	Plugins          *[]Plugin         `xml:"plugins>plugin,omitempty"`
}

type CIManagement

type CIManagement struct {
	System    string      `xml:"system,omitempty"`
	URL       string      `xml:"url,omitempty"`
	Notifiers *[]Notifier `xml:"notifiers>notifier,omitempty"`
}

type Configuration

type Configuration struct {
	Children         string `xml:"combine.children,attr,omitempty"`
	Self             string `xml:"combine.self,attr,omitempty"`
	RawConfiguration string `xml:",innerxml"`
}

Configuration is a raw XML configuration that we currently do not muck with. It's supposed to be a DOM object, but upstream uses map[string]string for properties, and it does not work. For now, just keep it as a string so we can marshal it out untouched. TODO: This should be a DOM object.

type Contributor

type Contributor struct {
	Name            string      `xml:"name,omitempty"`
	Email           string      `xml:"email,omitempty"`
	URL             string      `xml:"url,omitempty"`
	Organization    string      `xml:"organization,omitempty"`
	OrganizationURL string      `xml:"organizationUrl,omitempty"`
	Roles           *[]string   `xml:"roles>role,omitempty"`
	Timezone        string      `xml:"timezone,omitempty"`
	Properties      *Properties `xml:"properties,omitempty"`
}

type Dependency

type Dependency struct {
	GroupID    string       `xml:"groupId,omitempty"`
	ArtifactID string       `xml:"artifactId,omitempty"`
	Version    string       `xml:"version,omitempty"`
	Type       string       `xml:"type,omitempty"`
	Classifier string       `xml:"classifier,omitempty"`
	Scope      string       `xml:"scope,omitempty"`
	SystemPath string       `xml:"systemPath,omitempty"`
	Exclusions *[]Exclusion `xml:"exclusions>exclusion,omitempty"`
	Optional   string       `xml:"optional,omitempty"`
}

type DependencyManagement

type DependencyManagement struct {
	Dependencies *[]Dependency `xml:"dependencies>dependency,omitempty"`
}

type Developer

type Developer struct {
	ID              string      `xml:"id,omitempty"`
	Name            string      `xml:"name,omitempty"`
	Email           string      `xml:"email,omitempty"`
	URL             string      `xml:"url,omitempty"`
	Organization    string      `xml:"organization,omitempty"`
	OrganizationURL string      `xml:"organizationUrl,omitempty"`
	Roles           *[]string   `xml:"roles>role,omitempty"`
	Timezone        string      `xml:"timezone,omitempty"`
	Properties      *Properties `xml:"properties,omitempty"`
}

type DistributionManagement

type DistributionManagement struct {
	Repository         *Repository `xml:"repository,omitempty"`
	SnapshotRepository *Repository `xml:"snapshotRepository,omitempty"`
	Site               *Site       `xml:"site,omitempty"`
	DownloadURL        string      `xml:"downloadUrl,omitempty"`
	Relocation         *Relocation `xml:"relocation,omitempty"`
	Status             string      `xml:"status,omitempty"`
}

type Exclusion

type Exclusion struct {
	GroupID    string `xml:"groupId,omitempty"`
	ArtifactID string `xml:"artifactId,omitempty"`
}

type Extension

type Extension struct {
	GroupID    string `xml:"groupId,omitempty"`
	ArtifactID string `xml:"artifactId,omitempty"`
	Version    string `xml:"version,omitempty"`
}

type IssueManagement

type IssueManagement struct {
	System string `xml:"system,omitempty"`
	URL    string `xml:"url,omitempty"`
}

type License

type License struct {
	Name         string `xml:"name,omitempty"`
	URL          string `xml:"url,omitempty"`
	Distribution string `xml:"distribution,omitempty"`
	Comments     string `xml:"comments,omitempty"`
}

type MailingList

type MailingList struct {
	Name          string    `xml:"name,omitempty"`
	Subscribe     string    `xml:"subscribe,omitempty"`
	Unsubscribe   string    `xml:"unsubscribe,omitempty"`
	Post          string    `xml:"post,omitempty"`
	Archive       string    `xml:"archive,omitempty"`
	OtherArchives *[]string `xml:"otherArchives>otherArchive,omitempty"`
}

type Notifier

type Notifier struct {
	Type          string         `xml:"type,omitempty"`
	SendOnError   bool           `xml:"sendOnError,omitempty"`
	SendOnFailure bool           `xml:"sendOnFailure,omitempty"`
	SendOnSuccess bool           `xml:"sendOnSuccess,omitempty"`
	SendOnWarning bool           `xml:"sendOnWarning,omitempty"`
	Address       string         `xml:"address,omitempty"`
	Configuration *Configuration `xml:"configuration,omitempty"`
}

type Organization

type Organization struct {
	Name string `xml:"name,omitempty"`
	URL  string `xml:"url,omitempty"`
}

type Parent

type Parent struct {
	GroupID      string `xml:"groupId,omitempty"`
	ArtifactID   string `xml:"artifactId,omitempty"`
	Version      string `xml:"version,omitempty"`
	RelativePath string `xml:"relativePath,omitempty"`
}

type Plugin

type Plugin struct {
	GroupID       string             `xml:"groupId,omitempty"`
	ArtifactID    string             `xml:"artifactId,omitempty"`
	Version       string             `xml:"version,omitempty"`
	Extensions    string             `xml:"extensions,omitempty"`
	Executions    *[]PluginExecution `xml:"executions>execution,omitempty"`
	Dependencies  *[]Dependency      `xml:"dependencies>dependency,omitempty"`
	Inherited     string             `xml:"inherited,omitempty"`
	Configuration *Configuration     `xml:"configuration,omitempty"`
}

type PluginExecution

type PluginExecution struct {
	ID            string         `xml:"id,omitempty"`
	Phase         string         `xml:"phase,omitempty"`
	Goals         *[]string      `xml:"goals>goal,omitempty"`
	Inherited     string         `xml:"inherited,omitempty"`
	Configuration *Configuration `xml:"configuration,omitempty"`
}

type PluginManagement

type PluginManagement struct {
	Plugins *[]Plugin `xml:"plugins>plugin,omitempty"`
}

type PluginRepository

type PluginRepository struct {
	Releases  *RepositoryPolicy `xml:"releases,omitempty"`
	Snapshots *RepositoryPolicy `xml:"snapshots,omitempty"`
	ID        string            `xml:"id,omitempty"`
	Name      string            `xml:"name,omitempty"`
	URL       string            `xml:"url,omitempty"`
	Layout    string            `xml:"layout,omitempty"`
}

type Prerequisites

type Prerequisites struct {
	Maven string `xml:"maven,omitempty"`
}

type Profile

type Profile struct {
	ID                     string                  `xml:"id,omitempty"`
	Activation             *Activation             `xml:"activation,omitempty"`
	Build                  *BuildBase              `xml:"build,omitempty"`
	Modules                *[]string               `xml:"modules>module,omitempty"`
	DistributionManagement *DistributionManagement `xml:"distributionManagement,omitempty"`
	Properties             *Properties             `xml:"properties,omitempty"`
	DependencyManagement   *DependencyManagement   `xml:"dependencyManagement,omitempty"`
	Dependencies           *[]Dependency           `xml:"dependencies>dependency,omitempty"`
	Repositories           *[]Repository           `xml:"repositories>repository,omitempty"`
	PluginRepositories     *[]PluginRepository     `xml:"pluginRepositories>pluginRepository,omitempty"`
	Reporting              *Reporting              `xml:"reporting,omitempty"`
}

type Project

type Project struct {
	XMLName xml.Name `xml:"project,omitempty"`
	Xmlns   string   `xml:"xmlns,attr"`
	Xsi     string   `xml:"xsi,attr,omitempty"`
	// This is the variant of the above. In the POM, it's `xmlns:xsi`,
	// it gets parsed into `xsi`, so we have this so that when we
	// marshal the attribute has the right name. I'm sure it's something that's
	// wrong, but I don't know how to fix this.
	// Previous version had entirely separate struct for Marshalling,
	XsiNS          string `xml:"xmlns:xsi,attr,omitempty"`
	SchemaLocation string `xml:"schemaLocation,attr,omitempty"`
	// This is the variant of the above. In the POM, it's `xsi:schemaLocation`,
	// it gets parsed into `schemaLocation`, so we have this so that when we
	// marshal the attribute has the right name. I'm sure it's something that's
	// wrong, but I don't know how to fix this.
	SchemaLocationXSI      string                  `xml:"xsi:schemaLocation,attr,omitempty"`
	ModelVersion           string                  `xml:"modelVersion,omitempty"`
	GroupID                string                  `xml:"groupId,omitempty"`
	ArtifactID             string                  `xml:"artifactId,omitempty"`
	Version                string                  `xml:"version,omitempty"`
	Packaging              string                  `xml:"packaging,omitempty"`
	Name                   string                  `xml:"name,omitempty"`
	Description            string                  `xml:"description,omitempty"`
	URL                    string                  `xml:"url,omitempty"`
	InceptionYear          string                  `xml:"inceptionYear,omitempty"`
	Organization           *Organization           `xml:"organization,omitempty"`
	Licenses               *[]License              `xml:"licenses>license,omitempty"`
	Developers             *[]Developer            `xml:"developers>developer,omitempty"`
	Contributors           *[]Contributor          `xml:"contributors>contributor,omitempty"`
	MailingLists           *[]MailingList          `xml:"mailingLists>mailingList,omitempty"`
	Prerequisites          *Prerequisites          `xml:"prerequisites,omitempty"`
	Properties             *Properties             `xml:"properties,omitempty"`
	Parent                 *Parent                 `xml:"parent,omitempty"`
	Modules                *[]string               `xml:"modules>module,omitempty"`
	SCM                    *Scm                    `xml:"scm,omitempty"`
	IssueManagement        *IssueManagement        `xml:"issueManagement,omitempty"`
	CIManagement           *CIManagement           `xml:"ciManagement,omitempty"`
	DistributionManagement *DistributionManagement `xml:"distributionManagement,omitempty"`
	DependencyManagement   *DependencyManagement   `xml:"dependencyManagement,omitempty"`
	Dependencies           *[]Dependency           `xml:"dependencies>dependency,omitempty"`
	Repositories           *[]Repository           `xml:"repositories>repository,omitempty"`
	PluginRepositories     *[]PluginRepository     `xml:"pluginRepositories>pluginRepository,omitempty"`
	Build                  *Build                  `xml:"build,omitempty"`
	Reporting              *Reporting              `xml:"reporting,omitempty"`
	Profiles               *[]Profile              `xml:"profiles>profile,omitempty"`
}

func Parse

func Parse(path string) (*Project, error)

func (*Project) Marshal

func (p *Project) Marshal() ([]byte, error)

Marshal marshals the Project struct into a byte slice. Note that you must use this to get the correct XML output, as the attributes require special handling. Or there's a bug in the struct definition, but I don't know what it is.

type Properties

type Properties struct {
	Entries map[string]string
	// To preserve ordering when marshalling, we need to keep track of the
	// order. I'm sure there's a better way to do this, but this will suffice
	// for now.
	Order []string
}

func (Properties) MarshalXML

func (p Properties) MarshalXML(e *xml.Encoder, start xml.StartElement) error

func (*Properties) UnmarshalXML

func (p *Properties) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error)

type Relocation

type Relocation struct {
	GroupID    string `xml:"groupId,omitempty"`
	ArtifactID string `xml:"artifactId,omitempty"`
	Version    string `xml:"version,omitempty"`
	Message    string `xml:"message,omitempty"`
}

type ReportSet

type ReportSet struct {
	ID        string    `xml:"id,omitempty"`
	Reports   *[]string `xml:"reports>report,omitempty"`
	Inherited string    `xml:"inherited,omitempty"`
}

type Reporting

type Reporting struct {
	ExcludeDefaults string             `xml:"excludeDefaults,omitempty"`
	OutputDirectory string             `xml:"outputDirectory,omitempty"`
	Plugins         *[]ReportingPlugin `xml:"plugins>plugin,omitempty"`
}

type ReportingPlugin

type ReportingPlugin struct {
	GroupID    string       `xml:"groupId,omitempty"`
	ArtifactID string       `xml:"artifactId,omitempty"`
	Version    string       `xml:"version,omitempty"`
	Inherited  string       `xml:"inherited,omitempty"`
	ReportSets *[]ReportSet `xml:"reportSets>reportSet,omitempty"`
}

type Repository

type Repository struct {
	UniqueVersion bool              `xml:"uniqueVersion,omitempty"`
	Releases      *RepositoryPolicy `xml:"releases,omitempty"`
	Snapshots     *RepositoryPolicy `xml:"snapshots,omitempty"`
	ID            string            `xml:"id,omitempty"`
	Name          string            `xml:"name,omitempty"`
	URL           string            `xml:"url,omitempty"`
	Layout        string            `xml:"layout,omitempty"`
}

type RepositoryPolicy

type RepositoryPolicy struct {
	Enabled        string `xml:"enabled,omitempty"`
	UpdatePolicy   string `xml:"updatePolicy,omitempty"`
	ChecksumPolicy string `xml:"checksumPolicy,omitempty"`
}

type Resource

type Resource struct {
	TargetPath string    `xml:"targetPath,omitempty"`
	Filtering  string    `xml:"filtering,omitempty"`
	Directory  string    `xml:"directory,omitempty"`
	Includes   *[]string `xml:"includes>include,omitempty"`
	Excludes   *[]string `xml:"excludes>exclude,omitempty"`
}

type Scm

type Scm struct {
	Connection          string `xml:"connection,omitempty"`
	DeveloperConnection string `xml:"developerConnection,omitempty"`
	Tag                 string `xml:"tag,omitempty"`
	URL                 string `xml:"url,omitempty"`
}

type Site

type Site struct {
	ID   string `xml:"id,omitempty"`
	Name string `xml:"name,omitempty"`
	URL  string `xml:"url,omitempty"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL