github.phpd.cn/thought-machine/please@v12.2.0+incompatible/tools/please_maven/main.go (about)

     1  // Package main implements please_maven, a command-line tool to find dependencies
     2  // from a remote Maven repo (typically Maven Central, but can be others).
     3  //
     4  // This is a fairly non-trivial task since the pom.xml format is complex and
     5  // Maven is basically just a static file server for them. We do our best at
     6  // understanding it.
     7  // Of course other packages exist that can parse it, but we prefer not to use them
     8  // since they're Java, and would require shipping a very large binary, but
     9  // more significantly it did not seem easy to make them behave as we wanted.
    10  package main
    11  
    12  import (
    13  	"fmt"
    14  	"strings"
    15  
    16  	"cli"
    17  	"tools/please_maven/maven"
    18  )
    19  
    20  var opts = struct {
    21  	Usage        string
    22  	Repositories []string `short:"r" long:"repository" description:"Location of Maven repo" default:"https://repo1.maven.org/maven2"`
    23  	Android      bool     `short:"a" long:"android" description:"Adds https://maven.google.org to repositories for Android deps."`
    24  	Verbosity    int      `short:"v" long:"verbose" default:"1" description:"Verbosity of output (higher number = more output, default 1 -> warnings and errors only)"`
    25  	Exclude      []string `short:"e" long:"exclude" description:"Artifacts to exclude from download"`
    26  	Indent       bool     `short:"i" long:"indent" description:"Indent stdout lines appropriately"`
    27  	Optional     []string `short:"o" long:"optional" description:"Optional dependencies to fetch"`
    28  	BuildRules   bool     `short:"b" long:"build_rules" description:"Print individual maven_jar build rules for each artifact"`
    29  	NumThreads   int      `short:"n" long:"num_threads" default:"10" description:"Number of concurrent fetches to perform"`
    30  	LicenceOnly  bool     `short:"l" long:"licence_only" description:"Fetch only the licence of the given package from Maven"`
    31  	Args         struct {
    32  		Artifacts []maven.Artifact `positional-arg-name:"ids" required:"yes" description:"Maven IDs to fetch (e.g. io.grpc:grpc-all:1.4.0)"`
    33  	} `positional-args:"yes" required:"yes"`
    34  }{
    35  	Usage: `
    36  please_maven is a tool shipped with Please that communicates with Maven repositories
    37  to work out what files to download given a package spec.
    38  
    39  Example usage:
    40  please_maven io.grpc:grpc-all:1.1.2
    41  > io.grpc:grpc-auth:1.1.2:src:BSD 3-Clause
    42  > io.grpc:grpc-core:1.1.2:src:BSD 3-Clause
    43  > ...
    44  Its output is similarly in the common Maven artifact format which can be used to create
    45  maven_jar rules in BUILD files. It also outputs some notes on whether sources are
    46  available and what licence the package is under, if it can find it.
    47  
    48  Note that it does not do complex cross-package dependency resolution and doesn't
    49  necessarily support every aspect of Maven's pom.xml format, which is pretty hard
    50  to fully grok. The goal is to provide a backend to Please's built-in maven_jars
    51  rule to make adding dependencies easier.
    52  `,
    53  }
    54  
    55  func main() {
    56  	cli.ParseFlagsOrDie("please_maven", "9.0.3", &opts)
    57  	cli.InitLogging(opts.Verbosity)
    58  	if opts.Android {
    59  		opts.Repositories = append(opts.Repositories, "https://maven.google.com")
    60  	}
    61  	f := maven.NewFetch(opts.Repositories, opts.Exclude, opts.Optional)
    62  	if opts.LicenceOnly {
    63  		for _, artifact := range opts.Args.Artifacts {
    64  			for _, licence := range f.Pom(&artifact).Licences.Licence {
    65  				fmt.Println(licence.Name)
    66  			}
    67  		}
    68  	} else {
    69  		fmt.Println(strings.Join(maven.AllDependencies(f, opts.Args.Artifacts, opts.NumThreads, opts.Indent, opts.BuildRules), "\n"))
    70  	}
    71  }