github.com/joshdk/godel@v0.0.0-20170529232908-862138a45aee/apps/distgo/cmd/publish/pom.go (about)

     1  // Copyright 2016 Palantir Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package publish
    16  
    17  import (
    18  	"bytes"
    19  	"text/template"
    20  
    21  	"github.com/pkg/errors"
    22  
    23  	"github.com/palantir/godel/apps/distgo/templating"
    24  )
    25  
    26  const pomTemplate = `<?xml version="1.0" encoding="UTF-8"?>
    27  <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    28  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    29  <modelVersion>4.0.0</modelVersion>
    30  <groupId>{{.Publish.GroupID}}</groupId>
    31  <artifactId>{{.ProductName}}</artifactId>
    32  <version>{{.ProductVersion}}</version>
    33  <packaging>{{packagingType}}</packaging>
    34  </project>
    35  `
    36  
    37  func generatePOM(cfg templating.Config, distType string) ([]byte, error) {
    38  	funcs := template.FuncMap{
    39  		"packagingType": func() string { return distType },
    40  	}
    41  	t := template.Must(template.New("pom").Funcs(funcs).Parse(pomTemplate))
    42  
    43  	pomFileBuf := bytes.Buffer{}
    44  	if err := t.Execute(&pomFileBuf, cfg); err != nil {
    45  		return nil, errors.Wrapf(err, "failed to execute template")
    46  	}
    47  	return pomFileBuf.Bytes(), nil
    48  }