raydac / mvn-golang

maven plugin to automate GoSDK load and build of projects
Apache License 2.0
163 stars 31 forks source link

`go get` from artfactory #56

Closed sunnylbk closed 5 years ago

sunnylbk commented 5 years ago

Thanks a lot for this plugin.

Many of the artifacts that we use are internal packages which reside in artifactory. Is there a way to configure go get to use artifactory so that packages can also be downloaded from there?

Thanks

raydac commented 5 years ago

I didn't catch what do you mean? as I know go get just takes link to package and load it, for instance bitbucket.org/raydac/mvn-plugin-cvs-hg-test will load package from bitbucket.org and github.com/raydac/mvn-golang-cvs-test from github.com, you can place your package in any place with CVS supported by golang (it supports HG, GIT, SVN, BAZAAR) and use link to your repository in go get p.s. artifactory also supports golang agent since 1.11

sunnylbk commented 5 years ago

I am trying to resolve few libraries from artifactory which contains builds of private repos. default-get goal which runs bin/go get ... command doesn't look here. I currently resolve them using jfrog rt go build command as described in the link you provided. Is there a way to specify get goal to look into private artifactory by providing necessary credentials?

Thanks

raydac commented 5 years ago

ok, I will investigate the approach

sunnylbk commented 5 years ago

Thanks

raydac commented 5 years ago

I have added way to make calls to JFrog CLI tool with tuned GoSDK environment you can take a look at example the tool works with go.mod so that my default-get should not be used in the case as I see

sunnylbk commented 5 years ago

Many thanks for this!

I am encountering an error when trying to build with a private library from artifactory. The following command from terminal in src folder works fine and I am able to see a binary

bharaths-MacBook-Pro:src bharath$ /Users/bharath/Work/me/Development/jfrog-resolve-go/jfrogcli/jfrog-mac rt go build go --url=<replace-me> --user=<replace-me> --password=<replace-me>
[Info] Using go: go version go1.11.4 darwin/amd64

bharaths-MacBook-Pro:src bharath$ ls
go.mod  go.sum  hello   main.go

But when running with maven, it fails with following

[INFO] --- mvn-golang-wrapper:2.2.1-SNAPSHOT:jfrog-cli (call-jfrog-cli) @ mvn-golang-example-jfrog-cli ---
[WARNING] Value GOPATH is replaced by environment value.
[INFO] JFrog CLI: /Users/bharath/Work/me/Development/jfrog-resolve-go/jfrogcli/jfrog-mac
[INFO]    Target: rt
[INFO]   Command: go
[INFO]
[INFO] ---------Exec.Out---------
[INFO] main.go:5:2: cannot find package "github.com/me/drone-ci-go" in any of:
[INFO]  /usr/local/opt/go/libexec/src/github.com/me/drone-ci-go (from $GOROOT)
[INFO]  /Users/bharath/.go/src/github.com/me/drone-ci-go (from $GOPATH)
[INFO]  /Users/bharath/Work/me/Development/jfrog-resolve-go/src/github.com/me/drone-ci-go
[INFO]
[INFO]   "status": "failure",
[INFO]   "totals": {
[INFO]     "success": 0,
[INFO]     "failure": 1
[INFO]   }
[INFO]
[INFO]

I have that package under $GOPATH/pkg/mod. The command executed with maven is the same as that executed from terminal. I am unsure why it's failing in the second case.

Any thoughts on this?

Thanks

sunnylbk commented 5 years ago

I did run with <useEnvVars>true</useEnvVars> to keep maven environment similar to as running from terminal.

This is the configuration I am working with

    <build>
        <sourceDirectory>${basedir}${file.separator}src</sourceDirectory>
        <directory>${basedir}${file.separator}bin</directory>
        <plugins>
            <plugin>
                <groupId>com.igormaznitsa</groupId>
                <artifactId>mvn-golang-wrapper</artifactId>
                <version>2.2.1-SNAPSHOT</version>
                <extensions>true</extensions>
                <configuration>
                    <useEnvVars>true</useEnvVars>
                    <goVersion>1.11.4</goVersion>
                </configuration>
                <executions>
                    <execution>
                        <id>call-jfrog-cli</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>jfrog-cli</goal>
                        </goals>
                        <configuration>
                            <cliPath>${jfrogcli.path}</cliPath>
                            <target>rt</target>
                            <command>go</command>
                            <arguments>
                                <arg>build</arg>
                                <arg>go</arg>
                                <arg>--url={change-me}</arg>
                                <arg>--user={change-me}</arg>
                                <arg>--password={change-me}</arg>
                                <arg>--build-name=my-build</arg>
                                <arg>--build-number=1</arg>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
raydac commented 5 years ago

try to add in common config <env><GO111MODULE>on</GO111MODULE></env>

sunnylbk commented 5 years ago

Ah, that did the trick. Thank you so much!!

raydac commented 5 years ago

great, keep me informed about issues with project build

sunnylbk commented 5 years ago

So, I have been using the plugin to retrieve from artifactory. It's working well locally. I was also able to get this working drone CI with the configuration below:

pipeline:
  build:
    image: maven
    commands:
      # Essential packages
      - apt-get update
      - apt-get install build-essential -y
      - export M2_HOME=/usr/share/maven
      - mvn clean deploy --settings .travis.settings.xml

where .travis.settings.xml has configuration related to artifactory.

Recently, we switched to Travis CI and using a similar config is resulting in errors while trying to retrieve go libraries. This is the configuration used for Travis

language: java
sudo: true
install: true
script:
  # Essential packages
  - sudo apt-get update
  - sudo apt-get install build-essential -y
  - export M2_HOME=/usr/local/maven
  - mvn clean deploy --settings .travis.settings.xml

It's resulting in errors like below

[INFO] --- mvn-golang-wrapper:2.2.1-SNAPSHOT:jfrog-cli (call-jfrog-cli) @ <repo> ---
[INFO] JFrog CLI: /home/travis/build/<team>/<repo>/../jfrogcli/jfrog-linux
[INFO]    Target: rt
[INFO]   Command: go
[INFO] 
[INFO] ---------Exec.Out---------
[INFO] main.go:10:2: cannot find package "github.com/aws/aws-lambda-go/lambda" in any of:
[INFO]  /home/travis/.mvnGoLang/go1.11.4.linux-amd64/src/github.com/aws/aws-lambda-go/lambda (from $GOROOT)
[INFO]  /home/travis/.mvnGoLang/.go_path/src/github.com/aws/aws-lambda-go/lambda (from $GOPATH)
[INFO]  /home/travis/build/<team>/<repo>/src/github.com/aws/aws-lambda-go/lambda

The same configuration works locally too.

Is there any gotcha for Travis? Please let me you know if you have an insight into this.

Thanks in advance.

sunnylbk commented 5 years ago

Ok, I was able to get around the issue by using go base image. Posting config here if anyone encounters similar issue.

language: go
sudo: false
install: true
script:
  - export M2_HOME=/usr/local/maven
  - if [ "$TRAVIS_BRANCH" == "master" ]; then
    mvn -U clean deploy --settings .travis.settings.xml;
    fi
sunnylbk commented 5 years ago

Have another question, can the install phase of the plugin be configured to generate an artifact that could be used as a go dependency in another repo? Thanks

raydac commented 5 years ago

as I see there was some problem with rights in travis, I didn't test plugin with travis so that if you find errors or ways to improve to work with the CI, I can make changes in plugin

during install my plugin just zip whole project sources and resources and save it into maven repository as archive with mvn-golang extension, it can't be used as a standard Go dependency because Go knows nothing about maven but I guess it is possible to load and unpack sources from the artifact through standard maven plugins like maven-dependency-plugin and even build them as another maven process through maven-invoker-plugin

raydac commented 5 years ago

I checked approach with maven dependency plugin and it looks working, but I had to change resulted installed artifact extension to zip because the dependency plugin processes extension and it knows nothing about mvn-golang type

sunnylbk commented 5 years ago

Thanks for the above idea. I riffed off your code and am able to automatically rename .golang to a .zip. Working example here. Am unpacking into GOPATH folder so that it's visible globally.

I am currently trying to integrate with mod files.

raydac commented 5 years ago

I have added auto-search of artifacts generated by mvn-golang plugin in dependencies, unpack them into folder, and adding them automatically into GOPATH, so it started work like regular maven projects

sunnylbk commented 5 years ago

Thanks!

I tried to add mod files into existing maven repository example, but encountering the following error

[INFO] Prepared command line : bin/go get ./...
[ERROR]
[ERROR] ---------Exec.Err---------
[ERROR] go build com/igormaznitsa/mvngotestmeta: no Go files in
[ERROR]

It did work before adding mod files. Code for this is here. Any thoughts on this?

raydac commented 5 years ago

it looks like that such error for turned on GO11MODULE during build, I will investigate

raydac commented 5 years ago
If GO111MODULE=on, then the go command requires the use of modules,
never consulting GOPATH. We refer to this as the command being
module-aware or running in "module-aware mode".

mvn-golang mainly works through specially prepared GOPATH variable and downloaded and unpacked dependencies from maven repository are added into GOPATH but GO11MODULE=on is turn off GOPATH mechanism and it breaks work as I see

as I see they are still reworking the module mechanism and it still works not well and stable with local situated repositories and code, may be it would be good to not use such mechanism if maven repository in use