diff --git a/debug/debug.Dockerfile b/debug/debug.Dockerfile index 0870c3d7..169c2735 100644 --- a/debug/debug.Dockerfile +++ b/debug/debug.Dockerfile @@ -1,22 +1,32 @@ FROM golang:1.19 as builder WORKDIR /analyzer-lsp -COPY ../cmd /analyzer-lsp/cmd -COPY ../engine /analyzer-lsp/engine -COPY ../output /analyzer-lsp/output -COPY ../jsonrpc2 /analyzer-lsp/jsonrpc2 -COPY ../lsp /analyzer-lsp/lsp -COPY ../parser /analyzer-lsp/parser -COPY ../provider /analyzer-lsp/provider -COPY ../tracing /analyzer-lsp/tracing -COPY ../external-providers /analyzer-lsp/external-providers -COPY ../go.mod /analyzer-lsp/go.mod -COPY ../go.sum /analyzer-lsp/go.sum -COPY ../Makefile /analyzer-lsp/Makefile - -# Install delve (go debugger) +COPY ../cmd /analyzer-lsp/cmd +COPY ../engine /analyzer-lsp/engine +COPY ../event /analyzer-lsp/event +COPY ../output /analyzer-lsp/output +COPY ../jsonrpc2 /analyzer-lsp/jsonrpc2 +COPY ../jsonrpc2_v2 /analyzer-lsp/jsonrpc2_v2 +COPY ../lsp /analyzer-lsp/lsp +COPY ../parser /analyzer-lsp/parser +COPY ../provider /analyzer-lsp/provider +COPY ../tracing /analyzer-lsp/tracing +COPY ../external-providers /analyzer-lsp/external-providers +COPY ../go.mod /analyzer-lsp/go.mod +COPY ../go.sum /analyzer-lsp/go.sum +COPY ../Makefile /analyzer-lsp/Makefile + RUN go install github.com/go-delve/delve/cmd/dlv@latest +FROM registry.access.redhat.com/ubi9/ubi-minimal:latest as yq-builder +RUN microdnf install -y wget tar xz gzip && \ + microdnf clean all +ARG TARGETARCH +ARG YQ_VERSION="v4.40.5" +ARG YQ_BINARY="yq_linux_${TARGETARCH}" +RUN wget "https://github.com/mikefarah/yq/releases/download/${YQ_VERSION}/${YQ_BINARY}.tar.gz" -O - | tar xz && \ + mv ${YQ_BINARY} /usr/bin/yq + RUN go build -gcflags="all=-N -l" -o konveyor-analyzer ./cmd/analyzer/main.go RUN go build -gcflags="all=-N -l" -o konveyor-analyzer-dep ./cmd/dep/main.go RUN cd external-providers/generic-external-provider && go mod edit -replace=github.com/konveyor/analyzer-lsp=../../ && go build -gcflags="all=-N -l" -o generic-external-provider main.go diff --git a/demo-dep-output.yaml b/demo-dep-output.yaml index 7cbd4b12..656f10dd 100644 --- a/demo-dep-output.yaml +++ b/demo-dep-output.yaml @@ -1494,6 +1494,138 @@ - konveyor.io/dep-source=open-source - konveyor.io/language=java prefix: file:///root/.m2/repository/org/yaml/snakeyaml/1.28 +- fileURI: file:///analyzer-lsp/examples/gradle-multi-project-example/build.gradle + provider: java + dependencies: + - name: antlr.antlr + version: 2.7.7 + indirect: true + - name: com.apple.AppleJavaExtensions + version: "1.4" + indirect: true + - name: com.beust.jcommander + version: "1.48" + indirect: true + - name: com.google.code.findbugs.bcel-findbugs + version: "6.0" + indirect: true + - name: com.google.code.findbugs.findbugs + version: 3.0.1 + - name: com.google.code.findbugs.jFormatString + version: 2.0.1 + indirect: true + - name: com.google.code.findbugs.jsr305 + version: 1.3.9 + indirect: true + - name: com.google.code.findbugs.jsr305 + version: 2.0.1 + indirect: true + - name: com.google.code.gson.gson + version: "2.5" + indirect: true + - name: com.google.errorprone.error_prone_annotations + version: 2.0.18 + indirect: true + - name: com.google.guava.guava + version: "23.0" + - name: com.google.guava.guava + version: 23.2-jre + indirect: true + - name: com.google.j2objc.j2objc-annotations + version: "1.1" + indirect: true + - name: com.puppycrawl.tools.checkstyle + version: "8.4" + - name: commons-beanutils.commons-beanutils + version: 1.9.3 + indirect: true + - name: commons-cli.commons-cli + version: "1.4" + indirect: true + - name: commons-collections.commons-collections + version: 3.2.2 + indirect: true + - name: commons-io.commons-io + version: "2.4" + indirect: true + - name: commons-lang.commons-lang + version: "2.6" + indirect: true + - name: dom4j.dom4j + version: 1.6.1 + indirect: true + - name: jaxen.jaxen + version: 1.1.6 + indirect: true + - name: junit.junit + version: "4.12" + - name: net.bytebuddy.byte-buddy + version: 1.7.4 + indirect: true + - name: net.bytebuddy.byte-buddy-agent + version: 1.7.4 + indirect: true + - name: net.java.dev.javacc.javacc + version: "5.0" + indirect: true + - name: net.jcip.jcip-annotations + version: "1.0" + indirect: true + - name: net.sf.saxon.Saxon-HE + version: 9.8.0-5 + indirect: true + - name: net.sourceforge.pmd.pmd-core + version: 5.6.1 + indirect: true + - name: net.sourceforge.pmd.pmd-java + version: 5.6.1 + - name: net.sourceforge.saxon.saxon + version: 9.1.0.8 + indirect: true + - name: org.antlr.antlr4-runtime + version: "4.7" + indirect: true + - name: org.apache.commons.commons-lang3 + version: "3.4" + indirect: true + - name: org.apache.logging.log4j.log4j-api + version: 2.9.1 + - name: org.apache.logging.log4j.log4j-core + version: 2.9.1 + - name: org.apache.logging.log4j.log4j-slf4j-impl + version: 2.9.1 + - name: org.codehaus.mojo.animal-sniffer-annotations + version: "1.14" + indirect: true + - name: org.hamcrest.hamcrest-core + version: "1.3" + indirect: true + - name: org.mockito.mockito-core + version: 2.11.0 + - name: org.objenesis.objenesis + version: "2.6" + indirect: true + - name: org.ow2.asm.asm + version: 5.0.2 + indirect: true + - name: org.ow2.asm.asm + version: 5.0.4 + indirect: true + - name: org.ow2.asm.asm-commons + version: 5.0.2 + indirect: true + - name: org.ow2.asm.asm-debug-all + version: 5.0.2 + indirect: true + - name: org.ow2.asm.asm-tree + version: 5.0.2 + indirect: true + - name: org.slf4j.slf4j-api + version: 1.7.25 + indirect: true + - name: xml-apis.xml-apis + version: 1.0.b2 + indirect: true - fileURI: file:///analyzer-lsp/examples/inclusion-tests/pom.xml provider: java dependencies: [] diff --git a/demo-output.yaml b/demo-output.yaml index c04b5877..80c1a0db 100644 --- a/demo-output.yaml +++ b/demo-output.yaml @@ -354,6 +354,30 @@ variables: name: sigs.k8s.io/structured-merge-diff/v4 version: v4.2.1 + java-gradle-project: + description: | + This rule looks for a class only present in the gradle project + category: mandatory + incidents: + - uri: file:///examples/gradle-multi-project-example/template-server/src/main/java/io/jeffchao/template/server/Server.java + message: Only incidents in gradle project should appear + codeSnip: " 1 package io.jeffchao.template.server;\n 2 \n 3 import java.io.IOException;\n 4 import java.io.OutputStream;\n 5 import java.net.InetSocketAddress;\n 6 \n 7 import com.sun.net.httpserver.HttpExchange;\n 8 import com.sun.net.httpserver.HttpHandler;\n 9 import com.sun.net.httpserver.HttpServer;\n10 \n11 public class Server {\n12 \n13 public static void main(String[] args) throws IOException {\n14 String portString = System.getenv(\"PORT\");\n15 int port = portString == null ? 8080 : Integer.valueOf(portString);\n16 HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);\n17 server.createContext(\"/\", new MyHandler());" + lineNumber: 7 + variables: + file: file:///examples/gradle-multi-project-example/template-server/src/main/java/io/jeffchao/template/server/Server.java + kind: Module + name: com.sun.net.httpserver.HttpExchange + package: io.jeffchao.template.server + - uri: file:///examples/gradle-multi-project-example/template-server/src/main/java/io/jeffchao/template/server/Server.java + message: Only incidents in gradle project should appear + codeSnip: "14 String portString = System.getenv(\"PORT\");\n15 int port = portString == null ? 8080 : Integer.valueOf(portString);\n16 HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);\n17 server.createContext(\"/\", new MyHandler());\n18 server.setExecutor(null); // creates a default executor\n19 server.start();\n20 }\n21 \n22 static class MyHandler implements HttpHandler {\n23 @Override\n24 public void handle(HttpExchange t) throws IOException {\n25 String response = \"Hello from Gradle!\";\n26 t.sendResponseHeaders(200, response.length());\n27 OutputStream os = t.getResponseBody();\n28 os.write(response.getBytes());\n29 os.close();\n30 }\n31 }\n32 }\n" + lineNumber: 24 + variables: + file: file:///examples/gradle-multi-project-example/template-server/src/main/java/io/jeffchao/template/server/Server.java + kind: Method + name: handle + package: io.jeffchao.template.server + effort: 3 java-inclusion-test: description: "This rule tests includedPaths config of the java provider. There should be two instances of this issue in the example app. \nWe are filtering one of them using includedPaths in provider config.\n" category: mandatory @@ -381,6 +405,13 @@ description: "" category: potential incidents: + - uri: file:///examples/gradle-multi-project-example/build.gradle + message: dependency junit.junit with 4.12 is bad and you should feel bad for using it + codeSnip: " 1 apply plugin: 'idea'\n 2 \n 3 ext {\n 4 log4jVersion = '2.9.1'\n 5 }\n 6 \n 7 buildscript {\n 8 repositories {\n 9 jcenter()\n10 }\n11 dependencies {" + lineNumber: 0 + variables: + name: junit.junit + version: "4.12" - uri: file:///examples/java/pom.xml message: dependency io.fabric8.kubernetes-client with 6.0.0 is bad and you should feel bad for using it codeSnip: "26 \n27 \n28 \n29 junit\n30 junit\n31 4.11\n32 test\n33 \n34 \n35 io.fabric8\n36 kubernetes-client\n37 6.0.0\n38 \n39 \n40 io.fabric8\n41 kubernetes-client-api\n42 6.0.0\n43 \n44 \n45 javax\n46 javaee-api" diff --git a/examples/golang/go.sum b/examples/golang/go.sum index 16ccc8cf..b5a4d86b 100644 --- a/examples/golang/go.sum +++ b/examples/golang/go.sum @@ -125,6 +125,7 @@ github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoD github.com/form3tech-oss/jwt-go v3.2.3+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= +github.com/getkin/kin-openapi v0.76.0/go.mod h1:660oXbgy5JFMKreazJaQTw7o+X00qeSyhcnluiMv+Xg= github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= diff --git a/external-providers/java-external-provider/Dockerfile b/external-providers/java-external-provider/Dockerfile index e2661f72..a9b27b51 100644 --- a/external-providers/java-external-provider/Dockerfile +++ b/external-providers/java-external-provider/Dockerfile @@ -22,4 +22,4 @@ COPY --from=builder /java-provider/java-external-provider /usr/local/bin/java-ex EXPOSE 14651 -ENTRYPOINT ["java-external-provider", "--port", "14651"] +ENTRYPOINT ["java-external-provider", "--port", "14651"] \ No newline at end of file diff --git a/external-providers/java-external-provider/examples/gradle-multi-project-example/.gitignore b/external-providers/java-external-provider/examples/gradle-multi-project-example/.gitignore new file mode 100644 index 00000000..0c740ded --- /dev/null +++ b/external-providers/java-external-provider/examples/gradle-multi-project-example/.gitignore @@ -0,0 +1,105 @@ +### Intellij+iml ### +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff: +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/dictionaries + +# Sensitive or high-churn files: +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.xml +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml + +# Gradle: +.idea/**/gradle.xml +.idea/**/libraries + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +### Intellij+iml Patch ### +# Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023 + +.idea/ + +*.iml +modules.xml +.idea/misc.xml +*.ipr + +### Java ### +# Compiled class file +*.class + +# Log file +*.log + +# Package Files # +*.jar +*.war +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +### macOS ### +*.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +### Gradle ### +.gradle +**/build/ + +# Ignore Gradle GUI config +gradle-app.setting + +# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) +!gradle-wrapper.jar + +# Cache of project +.gradletasknamecache diff --git a/external-providers/java-external-provider/examples/gradle-multi-project-example/LICENSE b/external-providers/java-external-provider/examples/gradle-multi-project-example/LICENSE new file mode 100644 index 00000000..25408ddc --- /dev/null +++ b/external-providers/java-external-provider/examples/gradle-multi-project-example/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) [year] [author] + +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. diff --git a/external-providers/java-external-provider/examples/gradle-multi-project-example/Procfile b/external-providers/java-external-provider/examples/gradle-multi-project-example/Procfile new file mode 100644 index 00000000..40dc36e6 --- /dev/null +++ b/external-providers/java-external-provider/examples/gradle-multi-project-example/Procfile @@ -0,0 +1,2 @@ +web: java -jar build/libs/template-server-all.jar +worker: java -jar build/libs/template-core-all.jar diff --git a/external-providers/java-external-provider/examples/gradle-multi-project-example/README.md b/external-providers/java-external-provider/examples/gradle-multi-project-example/README.md new file mode 100644 index 00000000..64e41321 --- /dev/null +++ b/external-providers/java-external-provider/examples/gradle-multi-project-example/README.md @@ -0,0 +1,53 @@ +# gradle-multi-project-example + +Basic gradle template with subprojects, deployable to Heroku as separate dyno processes. + +## What's included? + +1. Gradle Plugins + - application plugin + - shadowjar plugin +2. Code Style + - checkstyle + - findbugs + - pmd +3. General Libraries + - guava + - junit + - mockito + - log4j2 via slf4j +4. Multi-Project Gradle Setup + - see: [settings.gradle](settings.gradle) +5. Heroku Deployment + - see: [Procfile](Procfile), [stage.gradle](gradle/heroku/stage.gradle) + +## Development + +### Building + +``` +$ ./gradlew clean build +``` + +### Testing + +``` +$ ./gradlew clean test +``` + +### Building Deployment Artifacts + +``` +$ ./gradlew clean stage +``` + +### Running + +Use the Gradle [application plugin](https://docs.gradle.org/current/userguide/application_plugin.html). +However, `./gradlew run` will run applications in lexicographical order. +Instead, explicitly specify which subproject to run: + +``` +$ ./gradlew template-core:run +$ ./gradlew template-server:run +``` diff --git a/external-providers/java-external-provider/examples/gradle-multi-project-example/build.gradle b/external-providers/java-external-provider/examples/gradle-multi-project-example/build.gradle new file mode 100644 index 00000000..d81723d2 --- /dev/null +++ b/external-providers/java-external-provider/examples/gradle-multi-project-example/build.gradle @@ -0,0 +1,47 @@ +apply plugin: 'idea' + +ext { + log4jVersion = '2.9.1' +} + +buildscript { + repositories { + jcenter() + } + dependencies { + classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.1' + } +} + +allprojects { + repositories { + mavenLocal() + mavenCentral() // maven { url: 'http://jcenter.bintray.com' } + } +} + +apply from: file('gradle/check.gradle') +apply from: file('gradle/heroku/clean.gradle') + +subprojects { + apply plugin: 'com.github.johnrengelman.shadow' + apply plugin: 'java' + + group = "io.jeffchao.${rootProject.name}" + + dependencies { + implementation 'com.google.guava:guava:23.0' + + testImplementation 'junit:junit:4.12' + + compile "org.apache.logging.log4j:log4j-api:$log4jVersion" + compile "org.apache.logging.log4j:log4j-core:$log4jVersion" + compile "org.apache.logging.log4j:log4j-slf4j-impl:$log4jVersion" + + testCompile 'org.mockito:mockito-core:2.11.0' + + } + + apply from: file("$rootProject.projectDir/gradle/heroku/stage.gradle") + +} \ No newline at end of file diff --git a/external-providers/java-external-provider/examples/gradle-multi-project-example/codequality/checkstyle.xml b/external-providers/java-external-provider/examples/gradle-multi-project-example/codequality/checkstyle.xml new file mode 100644 index 00000000..2d6336e9 --- /dev/null +++ b/external-providers/java-external-provider/examples/gradle-multi-project-example/codequality/checkstyle.xml @@ -0,0 +1,238 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/external-providers/java-external-provider/examples/gradle-multi-project-example/gradle/check.gradle b/external-providers/java-external-provider/examples/gradle-multi-project-example/gradle/check.gradle new file mode 100644 index 00000000..19af4208 --- /dev/null +++ b/external-providers/java-external-provider/examples/gradle-multi-project-example/gradle/check.gradle @@ -0,0 +1,15 @@ +subprojects { + apply plugin: 'checkstyle' + checkstyle { + ignoreFailures = true + configFile = rootProject.file('codequality/checkstyle.xml') + toolVersion = '8.4' + } + + apply plugin: 'findbugs' + findbugs { + ignoreFailures = true + } + + apply plugin: 'pmd' +} diff --git a/external-providers/java-external-provider/examples/gradle-multi-project-example/gradle/heroku/clean.gradle b/external-providers/java-external-provider/examples/gradle-multi-project-example/gradle/heroku/clean.gradle new file mode 100644 index 00000000..67329835 --- /dev/null +++ b/external-providers/java-external-provider/examples/gradle-multi-project-example/gradle/heroku/clean.gradle @@ -0,0 +1,5 @@ +apply plugin: 'base' + +clean.doLast { + delete rootProject.buildDir +} \ No newline at end of file diff --git a/external-providers/java-external-provider/examples/gradle-multi-project-example/gradle/heroku/stage.gradle b/external-providers/java-external-provider/examples/gradle-multi-project-example/gradle/heroku/stage.gradle new file mode 100644 index 00000000..819e4e83 --- /dev/null +++ b/external-providers/java-external-provider/examples/gradle-multi-project-example/gradle/heroku/stage.gradle @@ -0,0 +1,8 @@ +task stage(dependsOn: ['clean', 'shadowJar']) + +task copyToLib(type: Copy) { + from "$buildDir/libs" + into "$rootProject.buildDir/libs" +} +copyToLib.dependsOn(shadowJar) +stage.dependsOn(copyToLib) \ No newline at end of file diff --git a/external-providers/java-external-provider/examples/gradle-multi-project-example/gradle/wrapper/gradle-wrapper.jar b/external-providers/java-external-provider/examples/gradle-multi-project-example/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 00000000..27768f1b Binary files /dev/null and b/external-providers/java-external-provider/examples/gradle-multi-project-example/gradle/wrapper/gradle-wrapper.jar differ diff --git a/external-providers/java-external-provider/examples/gradle-multi-project-example/gradle/wrapper/gradle-wrapper.properties b/external-providers/java-external-provider/examples/gradle-multi-project-example/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 00000000..7e4921d3 --- /dev/null +++ b/external-providers/java-external-provider/examples/gradle-multi-project-example/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Wed Nov 01 15:30:19 PDT 2017 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-4.3-all.zip diff --git a/external-providers/java-external-provider/examples/gradle-multi-project-example/gradlew b/external-providers/java-external-provider/examples/gradle-multi-project-example/gradlew new file mode 100755 index 00000000..cccdd3d5 --- /dev/null +++ b/external-providers/java-external-provider/examples/gradle-multi-project-example/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/external-providers/java-external-provider/examples/gradle-multi-project-example/gradlew.bat b/external-providers/java-external-provider/examples/gradle-multi-project-example/gradlew.bat new file mode 100644 index 00000000..e95643d6 --- /dev/null +++ b/external-providers/java-external-provider/examples/gradle-multi-project-example/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/external-providers/java-external-provider/examples/gradle-multi-project-example/settings.gradle b/external-providers/java-external-provider/examples/gradle-multi-project-example/settings.gradle new file mode 100644 index 00000000..9c10890a --- /dev/null +++ b/external-providers/java-external-provider/examples/gradle-multi-project-example/settings.gradle @@ -0,0 +1,4 @@ +rootProject.name = 'gradle-multi-project-example' + +include 'template-core' +include 'template-server' diff --git a/external-providers/java-external-provider/examples/gradle-multi-project-example/template-core/build.gradle b/external-providers/java-external-provider/examples/gradle-multi-project-example/template-core/build.gradle new file mode 100644 index 00000000..7fb7ee66 --- /dev/null +++ b/external-providers/java-external-provider/examples/gradle-multi-project-example/template-core/build.gradle @@ -0,0 +1,10 @@ +apply plugin: 'application' + +mainClassName = 'io.jeffchao.template.core.Core' + +dependencies { +} + +run.doFirst { + // Environment variables go here. +} \ No newline at end of file diff --git a/external-providers/java-external-provider/examples/gradle-multi-project-example/template-core/src/main/java/io/jeffchao/template/core/Core.java b/external-providers/java-external-provider/examples/gradle-multi-project-example/template-core/src/main/java/io/jeffchao/template/core/Core.java new file mode 100644 index 00000000..b430f2b1 --- /dev/null +++ b/external-providers/java-external-provider/examples/gradle-multi-project-example/template-core/src/main/java/io/jeffchao/template/core/Core.java @@ -0,0 +1,8 @@ +package io.jeffchao.template.core; + +public class Core { + + public static void main(String[] args) { + System.out.println("hello, template!"); + } +} \ No newline at end of file diff --git a/external-providers/java-external-provider/examples/gradle-multi-project-example/template-core/src/test/java/io/jeffchao/template/core/CoreTest.java b/external-providers/java-external-provider/examples/gradle-multi-project-example/template-core/src/test/java/io/jeffchao/template/core/CoreTest.java new file mode 100644 index 00000000..d88ab5a1 --- /dev/null +++ b/external-providers/java-external-provider/examples/gradle-multi-project-example/template-core/src/test/java/io/jeffchao/template/core/CoreTest.java @@ -0,0 +1,16 @@ +package io.jeffchao.template.core; + +import org.junit.After; +import org.junit.Before; + + +public class CoreTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } +} \ No newline at end of file diff --git a/external-providers/java-external-provider/examples/gradle-multi-project-example/template-server/build.gradle b/external-providers/java-external-provider/examples/gradle-multi-project-example/template-server/build.gradle new file mode 100644 index 00000000..47600b02 --- /dev/null +++ b/external-providers/java-external-provider/examples/gradle-multi-project-example/template-server/build.gradle @@ -0,0 +1,10 @@ +apply plugin: 'application' + +mainClassName = 'io.jeffchao.template.server.Server' + +dependencies { +} + +run.doFirst { + // Environment variables go here. +} diff --git a/external-providers/java-external-provider/examples/gradle-multi-project-example/template-server/src/main/java/io/jeffchao/template/server/Server.java b/external-providers/java-external-provider/examples/gradle-multi-project-example/template-server/src/main/java/io/jeffchao/template/server/Server.java new file mode 100644 index 00000000..cc6d373d --- /dev/null +++ b/external-providers/java-external-provider/examples/gradle-multi-project-example/template-server/src/main/java/io/jeffchao/template/server/Server.java @@ -0,0 +1,32 @@ +package io.jeffchao.template.server; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.InetSocketAddress; + +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; +import com.sun.net.httpserver.HttpServer; + +public class Server { + + public static void main(String[] args) throws IOException { + String portString = System.getenv("PORT"); + int port = portString == null ? 8080 : Integer.valueOf(portString); + HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); + server.createContext("/", new MyHandler()); + server.setExecutor(null); // creates a default executor + server.start(); + } + + static class MyHandler implements HttpHandler { + @Override + public void handle(HttpExchange t) throws IOException { + String response = "Hello from Gradle!"; + t.sendResponseHeaders(200, response.length()); + OutputStream os = t.getResponseBody(); + os.write(response.getBytes()); + os.close(); + } + } +} \ No newline at end of file diff --git a/external-providers/java-external-provider/examples/gradle-multi-project-example/template-server/src/test/java/io/jeffchao/template/server/ServerTest.java b/external-providers/java-external-provider/examples/gradle-multi-project-example/template-server/src/test/java/io/jeffchao/template/server/ServerTest.java new file mode 100644 index 00000000..6f63c10e --- /dev/null +++ b/external-providers/java-external-provider/examples/gradle-multi-project-example/template-server/src/test/java/io/jeffchao/template/server/ServerTest.java @@ -0,0 +1,16 @@ +package io.jeffchao.template.server; + +import org.junit.After; +import org.junit.Before; + + +public class ServerTest { + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } +} \ No newline at end of file diff --git a/external-providers/java-external-provider/go.mod b/external-providers/java-external-provider/go.mod index 19bbba6e..8229d63f 100644 --- a/external-providers/java-external-provider/go.mod +++ b/external-providers/java-external-provider/go.mod @@ -6,14 +6,16 @@ require ( github.com/go-logr/logr v1.4.1 github.com/konveyor/analyzer-lsp v0.4.0-alpha.1.0.20240520232004-8af6f5c84a59 github.com/swaggest/openapi-go v0.2.50 - github.com/vifraa/gopom v1.0.0 go.lsp.dev/uri v0.3.0 go.opentelemetry.io/otel v1.11.2 google.golang.org/grpc v1.62.1 // indirect gopkg.in/yaml.v2 v2.4.0 ) -require github.com/sirupsen/logrus v1.9.0 +require ( + github.com/sirupsen/logrus v1.9.0 + github.com/vifraa/gopom v1.0.0 +) require ( github.com/golang-jwt/jwt/v5 v5.2.1 // indirect @@ -34,8 +36,10 @@ require ( go.opentelemetry.io/otel/exporters/jaeger v1.11.2 // indirect go.opentelemetry.io/otel/sdk v1.11.2 // indirect go.opentelemetry.io/otel/trace v1.11.2 // indirect - golang.org/x/net v0.22.0 // indirect - golang.org/x/sys v0.18.0 // indirect + golang.org/x/net v0.24.0 // indirect + golang.org/x/sys v0.19.0 // indirect golang.org/x/text v0.14.0 // indirect google.golang.org/protobuf v1.33.1-0.20240408130810-98873a205002 // indirect ) + +replace github.com/konveyor/analyzer-lsp => ../../ diff --git a/external-providers/java-external-provider/go.sum b/external-providers/java-external-provider/go.sum index 27274d0c..5ed222b1 100644 --- a/external-providers/java-external-provider/go.sum +++ b/external-providers/java-external-provider/go.sum @@ -66,15 +66,19 @@ go.opentelemetry.io/otel/trace v1.11.2 h1:Xf7hWSF2Glv0DE3MH7fBHvtpSBsjcBUe5MYAmZ go.opentelemetry.io/otel/trace v1.11.2/go.mod h1:4N+yC7QEz7TTsG9BSRLNAa63eg5E06ObSbKPmxQ/pKA= golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc= golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= +golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 h1:AjyfHzEPEFp/NpvfN5g+KDla3EMojjhRVZc1i7cj+oM= +google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 h1:Jyp0Hsi0bmHXG6k9eATXoYtjd6e2UzZ1SCn/wIupY14= +google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17/go.mod h1:oQ5rr10WTTMvP4A36n8JpR1OrO1BEiV4f78CneXZxkA= google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80/go.mod h1:PAREbraiVEVGVdTZsVWjSbbTtSyGbAgIIvni8a8CD5s= -google.golang.org/grpc v1.62.1 h1:B4n+nfKzOICUXMgyrNd19h/I9oH0L1pizfk1d4zSgTk= +google.golang.org/grpc v1.61.0 h1:TOvOcuXn30kRao+gfcvsebNEa5iZIiLkisYEkf7R7o0= +google.golang.org/grpc v1.61.0/go.mod h1:VUbo7IFqmF1QtCAstipjG0GIoq49KvMe9+h1jFLBNJs= google.golang.org/grpc v1.62.1/go.mod h1:IWTG0VlJLCh1SkC58F7np9ka9mx/WNkjl4PGJaiq+QE= google.golang.org/protobuf v1.33.1-0.20240408130810-98873a205002 h1:V7Da7qt0MkY3noVANIMVBk28nOnijADeOR3i5Hcvpj4= google.golang.org/protobuf v1.33.1-0.20240408130810-98873a205002/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= diff --git a/external-providers/java-external-provider/pkg/java_external_provider/dependency.go b/external-providers/java-external-provider/pkg/java_external_provider/dependency.go index d980e85e..4171036b 100644 --- a/external-providers/java-external-provider/pkg/java_external_provider/dependency.go +++ b/external-providers/java-external-provider/pkg/java_external_provider/dependency.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "context" + "errors" "fmt" "io" "io/fs" @@ -36,6 +37,21 @@ const ( baseDepKey = "baseDep" ) +const ( + maven = "maven" + gradle = "gradle" +) + +func (p *javaServiceClient) GetBuildTool() string { + bf := "" + if bf = p.findPom(); bf != "" { + return maven + } else if bf = p.findGradleBuild(); bf != "" { + return gradle + } + return "" +} + // TODO implement this for real func (p *javaServiceClient) findPom() string { var depPath string @@ -51,10 +67,45 @@ func (p *javaServiceClient) findPom() string { if err != nil { return "" } + if _, err := os.Stat(f); errors.Is(err, os.ErrNotExist) { + return "" + } return f } +func (p *javaServiceClient) findGradleBuild() string { + if p.config.Location != "" { + path := filepath.Join(p.config.Location, "build.gradle") + _, err := os.Stat(path) + if err != nil { + return "" + } + f, err := filepath.Abs(path) + if err != nil { + return "" + } + return f + } + return "" +} + func (p *javaServiceClient) GetDependencies(ctx context.Context) (map[uri.URI][]*provider.Dep, error) { + if p.GetBuildTool() == gradle { + p.log.V(2).Info("gradle found - retrieving dependencies") + m := map[uri.URI][]*provider.Dep{} + deps, err := p.getDependenciesForGradle(ctx) + for f, ds := range deps { + deps := []*provider.Dep{} + for _, dep := range ds { + d := dep.Dep + deps = append(deps, &d) + deps = append(deps, provider.ConvertDagItemsToList(dep.AddedDeps)...) + } + m[f] = deps + } + return m, err + } + p.depsMutex.RLock() val := p.depsCache p.depsMutex.RUnlock() @@ -226,6 +277,17 @@ func pomCoordinate(value *string) string { } func (p *javaServiceClient) GetDependenciesDAG(ctx context.Context) (map[uri.URI][]provider.DepDAGItem, error) { + switch p.GetBuildTool() { + case maven: + return p.getDependenciesForMaven(ctx) + case gradle: + return p.getDependenciesForGradle(ctx) + default: + return nil, fmt.Errorf("no build tool found") + } +} + +func (p *javaServiceClient) getDependenciesForMaven(ctx context.Context) (map[uri.URI][]provider.DepDAGItem, error) { localRepoPath := getMavenLocalRepoPath(p.mvnSettingsFile) path := p.findPom() @@ -274,6 +336,171 @@ func (p *javaServiceClient) GetDependenciesDAG(ctx context.Context) (map[uri.URI return m, nil } +// getDependenciesForGradle invokes the Gradle wrapper to get the dependency tree and returns all project dependencies +// TODO: what if no wrapper? +func (p *javaServiceClient) getDependenciesForGradle(ctx context.Context) (map[uri.URI][]provider.DepDAGItem, error) { + subprojects, err := p.getGradleSubprojects() + if err != nil { + return nil, err + } + + // command syntax: ./gradlew subproject1:dependencies subproject2:dependencies ... + args := []string{} + if len(subprojects) > 0 { + for _, sp := range subprojects { + args = append(args, fmt.Sprintf("%s:dependencies", sp)) + } + } else { + args = append(args, "dependencies") + } + + // get the graph output + exe, err := filepath.Abs(filepath.Join(p.config.Location, "gradlew")) + if err != nil { + return nil, fmt.Errorf("error calculating gradle wrapper path") + } + if _, err = os.Stat(exe); errors.Is(err, os.ErrNotExist) { + return nil, fmt.Errorf("a gradle wrapper must be present in the project") + } + cmd := exec.Command(exe, args...) + cmd.Dir = p.config.Location + output, err := cmd.CombinedOutput() + if err != nil { + return nil, err + } + + lines := strings.Split(string(output), "\n") + deps := p.parseGradleDependencyOutput(lines) + + // TODO: do we need to separate by submodule somehow? + + path := p.findGradleBuild() + file := uri.File(path) + m := map[uri.URI][]provider.DepDAGItem{} + m[file] = deps + + // TODO: need error? + return m, nil +} + +func (p *javaServiceClient) getGradleSubprojects() ([]string, error) { + args := []string{ + "projects", + } + + // Ideally we'd want to set this in gradle.properties, or as a -Dorg.gradle.java.home arg, + // but it doesn't seem to work in older Gradle versions. This should only affect child processes in any case. + err := os.Setenv("JAVA_HOME", os.Getenv("JAVA8_HOME")) + if err != nil { + return nil, err + } + + exe, err := filepath.Abs(filepath.Join(p.config.Location, "gradlew")) + if err != nil { + return nil, fmt.Errorf("error calculating gradle wrapper path") + } + if _, err = os.Stat(exe); errors.Is(err, os.ErrNotExist) { + return nil, fmt.Errorf("a gradle wrapper must be present in the project") + } + cmd := exec.Command(exe, args...) + cmd.Dir = p.config.Location + output, err := cmd.CombinedOutput() + if err != nil { + return nil, err + } + + beginRegex := regexp.MustCompile(`Root project`) + endRegex := regexp.MustCompile(`To see a list of`) + npRegex := regexp.MustCompile(`No sub-projects`) + pRegex := regexp.MustCompile(`.*- Project '(.*)'`) + + subprojects := []string{} + + gather := false + lines := strings.Split(string(output), "\n") + for _, line := range lines { + if npRegex.Find([]byte(line)) != nil { + return []string{}, nil + } + if beginRegex.Find([]byte(line)) != nil { + gather = true + continue + } + if gather { + if endRegex.Find([]byte(line)) != nil { + return subprojects, nil + } + + if p := pRegex.FindStringSubmatch(line); p != nil { + subprojects = append(subprojects, p[1]) + } + } + } + + return subprojects, fmt.Errorf("error parsing gradle dependency output") +} + +// parseGradleDependencyOutput converts the relevant lines from the dependency output into actual dependencies +// See https://regex101.com/r/9Gp7dW/1 for context +func (p *javaServiceClient) parseGradleDependencyOutput(lines []string) []provider.DepDAGItem { + deps := []provider.DepDAGItem{} + + treeDepRegex := regexp.MustCompile(`^([| ]+)?[+\\]--- (.*)`) + + // map of to + // this is so that children can be added to their respective parents + lastFoundWithDepth := make(map[int]*provider.DepDAGItem) + + for _, line := range lines { + match := treeDepRegex.FindStringSubmatch(line) + if match != nil { + dep := parseGradleDependencyString(match[2]) + if reflect.DeepEqual(dep, provider.DepDAGItem{}) { // ignore empty dependency + continue + } else if match[1] != "" { // transitive dependency + dep.Dep.Indirect = true + depth := len(match[1]) / 5 // get the level of anidation of the dependency within the tree + parent := lastFoundWithDepth[depth-1] // find its parent + parent.AddedDeps = append(parent.AddedDeps, dep) // add child to parent + lastFoundWithDepth[depth] = &parent.AddedDeps[len(parent.AddedDeps)-1] // update last found with given depth + } else { // root level (direct) dependency + deps = append(deps, dep) // add root dependency to result list + lastFoundWithDepth[0] = &deps[len(deps)-1] + continue + } + } + } + + return deps +} + +// parseGradleDependencyString parses the lines of the gradle dependency output, for instance: +// org.codehaus.groovy:groovy:3.0.21 +// org.codehaus.groovy:groovy:3.+ -> 3.0.21 +// com.codevineyard:hello-world:{strictly 1.0.1} -> 1.0.1 +// :simple-jar (n) +func parseGradleDependencyString(s string) provider.DepDAGItem { + // (*) - dependencies omitted (listed previously) + // (n) - Not resolved (configuration is not meant to be resolved) + if strings.HasSuffix(s, "(n)") || strings.HasSuffix(s, "(*)") { + return provider.DepDAGItem{} + } + + depRegex := regexp.MustCompile(`(.+):(.+):((.*) -> )?(.*)`) + libRegex := regexp.MustCompile(`:(.*)`) + + dep := provider.Dep{} + match := depRegex.FindStringSubmatch(s) + if match != nil { + dep.Name = match[1] + "." + match[2] + dep.Version = match[5] + } else if match = libRegex.FindStringSubmatch(s); match != nil { + dep.Name = match[1] + } + + return provider.DepDAGItem{Dep: dep, AddedDeps: []provider.DepDAGItem{}} +} + // extractSubmoduleTrees creates an array of lines for each submodule tree found in the mvn dependency:tree output func extractSubmoduleTrees(lines []string) [][]string { submoduleTrees := [][]string{} diff --git a/external-providers/java-external-provider/pkg/java_external_provider/dependency_test.go b/external-providers/java-external-provider/pkg/java_external_provider/dependency_test.go index d605f319..33957093 100644 --- a/external-providers/java-external-provider/pkg/java_external_provider/dependency_test.go +++ b/external-providers/java-external-provider/pkg/java_external_provider/dependency_test.go @@ -560,3 +560,238 @@ func Test_parseMavenDepLines(t *testing.T) { }) } } + +func Test_parseGradleDependencyOutput(t *testing.T) { + gradleOutput := ` +Starting a Gradle Daemon, 1 incompatible Daemon could not be reused, use --status for details + +> Task :dependencies + +------------------------------------------------------------ +Root project +------------------------------------------------------------ + +annotationProcessor - Annotation processors and their dependencies for source set 'main'. +No dependencies + +api - API dependencies for source set 'main'. (n) +No dependencies + +apiElements - API elements for main. (n) +No dependencies + +archives - Configuration for archive artifacts. (n) +No dependencies + +compileClasspath - Compile classpath for source set 'main'. ++--- org.codehaus.groovy:groovy:3.+ -> 3.0.21 ++--- org.codehaus.groovy:groovy-json:3.+ -> 3.0.21 +| \--- org.codehaus.groovy:groovy:3.0.21 ++--- com.codevineyard:hello-world:{strictly 1.0.1} -> 1.0.1 +\--- :simple-jar + +testRuntimeOnly - Runtime only dependencies for source set 'test'. (n) +No dependencies + +(*) - dependencies omitted (listed previously) + +(n) - Not resolved (configuration is not meant to be resolved) + +A web-based, searchable dependency report is available by adding the --scan option. + +BUILD SUCCESSFUL in 4s +1 actionable task: 1 executed +` + + lines := strings.Split(gradleOutput, "\n") + + p := javaServiceClient{ + log: testr.New(t), + depToLabels: map[string]*depLabelItem{}, + config: provider.InitConfig{ + ProviderSpecificConfig: map[string]interface{}{ + "excludePackages": []string{}, + }, + }, + } + + wantedDeps := []provider.DepDAGItem{ + { + Dep: provider.Dep{ + Name: "org.codehaus.groovy.groovy", + Version: "3.0.21", + Indirect: false, + }, + }, + { + Dep: provider.Dep{ + Name: "org.codehaus.groovy.groovy-json", + Version: "3.0.21", + Indirect: false, + }, + AddedDeps: []provider.DepDAGItem{ + { + Dep: provider.Dep{ + Name: "org.codehaus.groovy.groovy", + Version: "3.0.21", + Indirect: true, + }, + }, + }, + }, + { + Dep: provider.Dep{ + Name: "com.codevineyard.hello-world", + Version: "1.0.1", + Indirect: false, + }, + }, + { + Dep: provider.Dep{ + Name: "simple-jar", + Indirect: false, + }, + }, + } + + deps := p.parseGradleDependencyOutput(lines) + + if len(deps) != len(wantedDeps) { + t.Errorf("different number of dependencies found") + } + + for i := 0; i < len(deps); i++ { + dep := deps[i] + wantedDep := wantedDeps[i] + if dep.Dep.Name != wantedDep.Dep.Name { + t.Errorf("wanted name: %s, found name: %s", wantedDep.Dep.Name, dep.Dep.Name) + } + if dep.Dep.Version != wantedDep.Dep.Version { + t.Errorf("wanted version: %s, found version: %s", wantedDep.Dep.Version, dep.Dep.Version) + } + if len(dep.AddedDeps) != len(wantedDep.AddedDeps) { + t.Errorf("wanted %d child deps, found %d for dep %s", len(wantedDep.AddedDeps), len(dep.AddedDeps), dep.Dep.Name) + } + + } + +} + +func Test_parseGradleDependencyOutput_withTwoLevelsOfNesting(t *testing.T) { + gradleOutput := ` +Starting a Gradle Daemon, 1 incompatible Daemon could not be reused, use --status for details + +> Task :dependencies + +------------------------------------------------------------ +Root project +------------------------------------------------------------ + +annotationProcessor - Annotation processors and their dependencies for source set 'main'. +No dependencies + +api - API dependencies for source set 'main'. (n) +No dependencies + +apiElements - API elements for main. (n) +No dependencies + +archives - Configuration for archive artifacts. (n) +No dependencies + +compileClasspath - Compile classpath for source set 'main'. ++--- net.sourceforge.pmd:pmd-java:5.6.1 + +--- net.sourceforge.pmd:pmd-core:5.6.1 + | \--- com.google.code.gson:gson:2.5 + \--- net.sourceforge.saxon:saxon:9.1.0.8 ++--- org.apache.logging.log4j:log4j-api:2.9.1 + +testRuntimeOnly - Runtime only dependencies for source set 'test'. (n) +No dependencies + +(*) - dependencies omitted (listed previously) + +(n) - Not resolved (configuration is not meant to be resolved) + +A web-based, searchable dependency report is available by adding the --scan option. + +BUILD SUCCESSFUL in 4s +1 actionable task: 1 executed +` + + lines := strings.Split(gradleOutput, "\n") + + p := javaServiceClient{ + log: testr.New(t), + depToLabels: map[string]*depLabelItem{}, + config: provider.InitConfig{ + ProviderSpecificConfig: map[string]interface{}{ + "excludePackages": []string{}, + }, + }, + } + + wantedDeps := []provider.DepDAGItem{ + { + Dep: provider.Dep{ + Name: "net.sourceforge.pmd.pmd-java", + Version: "5.6.1", + Indirect: false, + }, + AddedDeps: []provider.DepDAGItem{ + { + Dep: provider.Dep{ + Name: "net.sourceforge.pmd.pmd-core", + Version: "5.6.1", + Indirect: true, + }, + AddedDeps: []provider.DepDAGItem{ + { + Dep: provider.Dep{ + Name: "com.google.code.gson.gson", + Version: "2.5", + Indirect: true, + }, + }, + }, + }, + { + Dep: provider.Dep{ + Name: "net.sourceforge.saxon.saxon", + Version: "9.1.0.8", + Indirect: true, + }, + }, + }, + }, + { + Dep: provider.Dep{ + Name: "org.apache.logging.log4j.log4j-api", + Version: "2.9.1", + Indirect: false, + }, + }, + } + + deps := p.parseGradleDependencyOutput(lines) + + if len(deps) != len(wantedDeps) { + t.Errorf("different number of dependencies found") + } + + for i := 0; i < len(deps); i++ { + dep := deps[i] + wantedDep := wantedDeps[i] + if dep.Dep.Name != wantedDep.Dep.Name { + t.Errorf("wanted name: %s, found name: %s", wantedDep.Dep.Name, dep.Dep.Name) + } + if dep.Dep.Version != wantedDep.Dep.Version { + t.Errorf("wanted version: %s, found version: %s", wantedDep.Dep.Version, dep.Dep.Version) + } + if len(dep.AddedDeps) != len(wantedDep.AddedDeps) { + t.Errorf("wanted %d child deps, found %d for dep %s", len(wantedDep.AddedDeps), len(dep.AddedDeps), dep.Dep.Name) + } + + } + +} diff --git a/external-providers/java-external-provider/pkg/java_external_provider/filter.go b/external-providers/java-external-provider/pkg/java_external_provider/filter.go index fd6d85b0..3e33d14d 100644 --- a/external-providers/java-external-provider/pkg/java_external_provider/filter.go +++ b/external-providers/java-external-provider/pkg/java_external_provider/filter.go @@ -263,7 +263,7 @@ func (p *javaServiceClient) getURI(refURI string) (string, uri.URI, error) { var jarPath string if sourceRange { - // If there is a source range, we know we know there is a sources jar + // If there is a source range, we know there is a sources jar jarName := filepath.Base(u.Path) s := strings.TrimSuffix(jarName, ".jar") s = fmt.Sprintf("%v-sources.jar", s) @@ -272,26 +272,61 @@ func (p *javaServiceClient) getURI(refURI string) (string, uri.URI, error) { jarName := filepath.Base(u.Path) jarPath = filepath.Join(filepath.Dir(u.Path), jarName) } - path := filepath.Join(strings.Split(strings.TrimSuffix(packageName, ".class"), ".")...) + + path := filepath.Join(strings.Split(strings.TrimSuffix(packageName, ".class"), ".")...) // path: org/apache/logging/log4j/core/appender/FileManager javaFileName := fmt.Sprintf("%s.java", filepath.Base(path)) if i := strings.Index(javaFileName, "$"); i > 0 { javaFileName = fmt.Sprintf("%v.java", javaFileName[0:i]) } - javaFileAbsolutePath := filepath.Join(filepath.Dir(jarPath), filepath.Dir(path), javaFileName) + javaFileAbsolutePath := "" + if p.GetBuildTool() == maven { + javaFileAbsolutePath = filepath.Join(filepath.Dir(jarPath), filepath.Dir(path), javaFileName) - // attempt to decompile when directory for the expected java file doesn't exist - // if directory exists, assume .java file is present within, this avoids decompiling every Jar - if _, err := os.Stat(filepath.Dir(javaFileAbsolutePath)); err != nil { - cmd := exec.Command("jar", "xf", filepath.Base(jarPath)) - cmd.Dir = filepath.Dir(jarPath) - err := cmd.Run() + // attempt to decompile when directory for the expected java file doesn't exist + // if directory exists, assume .java file is present within, this avoids decompiling every Jar + if _, err := os.Stat(filepath.Dir(javaFileAbsolutePath)); err != nil { + cmd := exec.Command("jar", "xf", filepath.Base(jarPath)) + cmd.Dir = filepath.Dir(jarPath) + err := cmd.Run() + if err != nil { + fmt.Printf("\n java error%v", err) + return "", "", err + } + } + } else if p.GetBuildTool() == gradle { + sourcesFile := "" + jarFile := filepath.Base(jarPath) + walker := func(path string, d os.DirEntry, err error) error { + if err != nil { + return fmt.Errorf("found error traversing files: %w", err) + } + if !d.IsDir() && d.Name() == jarFile { + sourcesFile = path + if err != nil { + return fmt.Errorf("found error traversing files: %w", err) + } + return nil + } + return nil + } + root := filepath.Join(jarPath, "..", "..") + err := filepath.WalkDir(root, walker) + if err != nil { + return "", "", err + } + javaFileAbsolutePath = filepath.Join(filepath.Dir(sourcesFile), filepath.Dir(path), javaFileName) + + cmd := exec.Command("jar", "xf", filepath.Base(sourcesFile)) + cmd.Dir = filepath.Dir(sourcesFile) + err = cmd.Run() if err != nil { fmt.Printf("\n java error%v", err) return "", "", err } } + ui := uri.New(javaFileAbsolutePath) file, err := os.Open(ui.Filename()) defer file.Close() diff --git a/external-providers/java-external-provider/pkg/java_external_provider/provider.go b/external-providers/java-external-provider/pkg/java_external_provider/provider.go index 1bedb85b..b6bbe975 100644 --- a/external-providers/java-external-provider/pkg/java_external_provider/provider.go +++ b/external-providers/java-external-provider/pkg/java_external_provider/provider.go @@ -4,6 +4,7 @@ import ( "bufio" "bytes" "context" + "errors" "fmt" "io" "os" @@ -234,16 +235,6 @@ func (p *javaProvider) Init(ctx context.Context, log logr.Logger, config provide isBinary = true } - if mode == provider.FullAnalysisMode { - // we attempt to decompile JARs of dependencies that don't have a sources JAR attached - // we need to do this for jdtls to correctly recognize source attachment for dep - err := resolveSourcesJars(ctx, log, config.Location, mavenSettingsFile) - if err != nil { - // TODO (pgaikwad): should we ignore this failure? - log.Error(err, "failed to resolve sources jar for location", "location", config.Location) - } - } - // handle proxy settings for k, v := range config.Proxy.ToEnvVars() { os.Setenv(k, v) @@ -311,6 +302,25 @@ func (p *javaProvider) Init(ctx context.Context, log logr.Logger, config provide includedPaths: provider.GetIncludedPathsFromConfig(config, false), } + if mode == provider.FullAnalysisMode { + // we attempt to decompile JARs of dependencies that don't have a sources JAR attached + // we need to do this for jdtls to correctly recognize source attachment for dep + switch svcClient.GetBuildTool() { + case maven: + err := resolveSourcesJarsForMaven(ctx, log, config.Location, mavenSettingsFile) + if err != nil { + // TODO (pgaikwad): should we ignore this failure? + log.Error(err, "failed to resolve maven sources jar for location", "location", config.Location) + } + case gradle: + err = resolveSourcesJarsForGradle(ctx, log, config.Location, mavenSettingsFile, &svcClient) + if err != nil { + log.Error(err, "failed to resolve gradle sources jar for location", "location", config.Location) + } + } + + } + svcClient.initialization(ctx) err = svcClient.depInit() if err != nil { @@ -319,6 +329,168 @@ func (p *javaProvider) Init(ctx context.Context, log logr.Logger, config provide return &svcClient, returnErr } +func resolveSourcesJarsForGradle(ctx context.Context, log logr.Logger, location string, mvnSettings string, svc *javaServiceClient) error { + ctx, span := tracing.StartNewSpan(ctx, "resolve-sources") + defer span.End() + + log.V(5).Info("resolving dependency sources for gradle") + + gb := svc.findGradleBuild() + if gb == "" { + return fmt.Errorf("could not find gradle build file for project") + } + + // create a temporary build file to append the task for downloading sources + taskgb := filepath.Join(filepath.Dir(gb), "tmp.gradle") + err := CopyFile(gb, taskgb) + if err != nil { + return fmt.Errorf("error copying file %s to %s", gb, taskgb) + } + defer os.Remove(taskgb) + + // append downloader task + taskfile := "/root/.gradle/task.gradle" + err = AppendToFile(taskfile, taskgb) + if err != nil { + return fmt.Errorf("error appending file %s to %s", taskfile, taskgb) + } + + tmpgbname := filepath.Join(location, "toberenamed.gradle") + err = os.Rename(gb, tmpgbname) + if err != nil { + return fmt.Errorf("error renaming file %s to %s", gb, "toberenamed.gradle") + } + defer os.Rename(tmpgbname, gb) + + err = os.Rename(taskgb, gb) + if err != nil { + return fmt.Errorf("error renaming file %s to %s", gb, "toberenamed.gradle") + } + defer os.Remove(gb) + + // run gradle wrapper with tmp build file + exe, err := filepath.Abs(filepath.Join(svc.config.Location, "gradlew")) + if err != nil { + return fmt.Errorf("error calculating gradle wrapper path") + } + if _, err = os.Stat(exe); errors.Is(err, os.ErrNotExist) { + return fmt.Errorf("a gradle wrapper must be present in the project") + } + + // gradle must run with java 8 (see compatibility matrix) + java8home := os.Getenv("JAVA8_HOME") + if java8home == "" { + return fmt.Errorf("") + } + + args := []string{ + "konveyorDownloadSources", + } + cmd := exec.CommandContext(ctx, exe, args...) + cmd.Env = append(cmd.Env, fmt.Sprintf("JAVA_HOME=%s", java8home)) + cmd.Dir = location + output, err := cmd.CombinedOutput() + if err != nil { + return err + } + + log.V(8).WithValues("output", output).Info("got gradle output") + + // TODO: what if all sources available + reader := bytes.NewReader(output) + unresolvedSources, err := parseUnresolvedSourcesForGradle(reader) + if err != nil { + return err + } + + fmt.Printf("%d", len(unresolvedSources)) + + decompileJobs := []decompileJob{} + if len(unresolvedSources) > 1 { + // Gradle cache dir structure changes over time - we need to find where the actual dependencies are stored + cache, err := findGradleCache(unresolvedSources[0].GroupId) + if err != nil { + return err + } + + for _, artifact := range unresolvedSources { + log.V(5).WithValues("artifact", artifact).Info("sources for artifact not found, decompiling...") + + artifactDir := filepath.Join(cache, artifact.GroupId, artifact.ArtifactId) + jarName := fmt.Sprintf("%s-%s.jar", artifact.ArtifactId, artifact.Version) + artifactPath, err := findGradleArtifact(artifactDir, jarName) + if err != nil { + return err + } + decompileJobs = append(decompileJobs, decompileJob{ + artifact: artifact, + inputPath: artifactPath, + outputPath: filepath.Join(filepath.Dir(artifactPath), "decompiled", jarName), + }) + } + err = decompile(ctx, log, alwaysDecompileFilter(true), 10, decompileJobs, "") + if err != nil { + return err + } + // move decompiled files to base location of the jar + for _, decompileJob := range decompileJobs { + jarName := strings.TrimSuffix(filepath.Base(decompileJob.inputPath), ".jar") + err = moveFile(decompileJob.outputPath, + filepath.Join(filepath.Dir(decompileJob.inputPath), + fmt.Sprintf("%s-sources.jar", jarName))) + if err != nil { + log.V(5).Error(err, "failed to move decompiled file", "file", decompileJob.outputPath) + } + } + + } + return nil +} + +// findGradleCache looks for the folder within the Gradle cache where the actual dependencies are stored +// by walking the cache directory looking for a directory equal to the given sample group id +func findGradleCache(sampleGroupId string) (string, error) { + // TODO(jmle): atm taking for granted that the cache is going to be here + root := "/root/.gradle/caches" + cache := "" + walker := func(path string, d os.DirEntry, err error) error { + if err != nil { + return fmt.Errorf("found error looking for cache directory: %w", err) + } + if d.IsDir() && d.Name() == sampleGroupId { + cache = path + return filepath.SkipAll + } + return nil + } + err := filepath.WalkDir(root, walker) + if err != nil { + return "", err + } + cache = filepath.Dir(cache) // return the parent of the found directory + return cache, nil +} + +// findGradleArtifact looks for a given artifact jar within the given root dir +func findGradleArtifact(root string, artifactId string) (string, error) { + artifactPath := "" + walker := func(path string, d os.DirEntry, err error) error { + if err != nil { + return fmt.Errorf("found error looking for artifact: %w", err) + } + if !d.IsDir() && d.Name() == artifactId { + artifactPath = path + return filepath.SkipAll + } + return nil + } + err := filepath.WalkDir(root, walker) + if err != nil { + return "", err + } + return artifactPath, nil +} + // GetLocation given a dep, attempts to find line number, caches the line number for a given dep func (j *javaProvider) GetLocation(ctx context.Context, dep konveyor.Dep, file string) (engine.Location, error) { location := engine.Location{StartPosition: engine.Position{}, EndPosition: engine.Position{}} @@ -385,9 +557,9 @@ func (j *javaProvider) GetLocation(ctx context.Context, dep konveyor.Dep, file s return location, nil } -// resolveSourcesJars for a given source code location, runs maven to find +// resolveSourcesJarsForMaven for a given source code location, runs maven to find // deps that don't have sources attached and decompiles them -func resolveSourcesJars(ctx context.Context, log logr.Logger, location, mavenSettings string) error { +func resolveSourcesJarsForMaven(ctx context.Context, log logr.Logger, location, mavenSettings string) error { // TODO (pgaikwad): when we move to external provider, inherit context from parent ctx, span := tracing.StartNewSpan(ctx, "resolve-sources") defer span.End() @@ -455,6 +627,50 @@ func resolveSourcesJars(ctx context.Context, log logr.Logger, location, mavenSet return nil } +// parseUnresolvedSources takes the output from the download sources gradle task and returns the artifacts whose sources +// could not be found. Sample gradle output: +// Found 0 sources for :simple-jar: +// Found 1 sources for com.codevineyard:hello-world:1.0.1 +// Found 1 sources for org.codehaus.groovy:groovy:3.0.21 +func parseUnresolvedSourcesForGradle(output io.Reader) ([]javaArtifact, error) { + unresolvedSources := []javaArtifact{} + unresolvedRegex := regexp.MustCompile(`Found 0 sources for (.*)`) + artifactRegex := regexp.MustCompile(`(.+):(.+):(.+)|:(.+):`) + + scanner := bufio.NewScanner(output) + for scanner.Scan() { + line := scanner.Text() + + if match := unresolvedRegex.FindStringSubmatch(line); len(match) != 0 { + gav := artifactRegex.FindStringSubmatch(match[1]) + if gav[4] != "" { // internal library, unknown group/version + artifact := javaArtifact{ + ArtifactId: match[4], + } + unresolvedSources = append(unresolvedSources, artifact) + } else { // external dependency + artifact := javaArtifact{ + GroupId: gav[1], + ArtifactId: gav[2], + Version: gav[3], + } + unresolvedSources = append(unresolvedSources, artifact) + } + } + } + + // dedup artifacts + result := []javaArtifact{} + for _, artifact := range unresolvedSources { + if contains(result, artifact) { + continue + } + result = append(result, artifact) + } + + return result, scanner.Err() +} + // parseUnresolvedSources takes the output from the go-offline maven plugin and returns the artifacts whose sources // could not be found. func parseUnresolvedSources(output io.Reader) ([]javaArtifact, error) { diff --git a/external-providers/java-external-provider/pkg/java_external_provider/service_client.go b/external-providers/java-external-provider/pkg/java_external_provider/service_client.go index 1749b7cb..ccb83704 100644 --- a/external-providers/java-external-provider/pkg/java_external_provider/service_client.go +++ b/external-providers/java-external-provider/pkg/java_external_provider/service_client.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "os" "os/exec" "path/filepath" "regexp" @@ -205,6 +206,8 @@ func (p *javaServiceClient) initialization(ctx context.Context) { params.ExtendedClientCapilities = map[string]interface{}{ "classFileContentsSupport": true, } + // See https://github.com/eclipse-jdtls/eclipse.jdt.ls/blob/1a3dd9323756113bf39cfab82746d57a2fd19474/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java + java8home := os.Getenv("JAVA8_HOME") params.InitializationOptions = map[string]interface{}{ "bundles": absBundles, "workspaceFolders": []string{fmt.Sprintf("file://%v", absLocation)}, @@ -221,6 +224,13 @@ func (p *javaServiceClient) initialization(ctx context.Context) { "maven": map[string]interface{}{ "downloadSources": downloadSources, }, + "import": map[string]interface{}{ + "gradle": map[string]interface{}{ + "java": map[string]interface{}{ + "home": java8home, + }, + }, + }, }, }, } diff --git a/external-providers/java-external-provider/pkg/java_external_provider/util.go b/external-providers/java-external-provider/pkg/java_external_provider/util.go index d8bb0f75..1952e824 100644 --- a/external-providers/java-external-provider/pkg/java_external_provider/util.go +++ b/external-providers/java-external-provider/pkg/java_external_provider/util.go @@ -131,9 +131,11 @@ func decompile(ctx context.Context, log logr.Logger, filter decompileFilter, wor "failed to create directories for decompiled file", "path", outputPathDir) continue } + // multiple java versions may be installed - chose $JAVA_HOME one + java := filepath.Join(os.Getenv("JAVA_HOME"), "bin", "java") // -mpm (max processing method) is required to keep decomp time low cmd := exec.CommandContext( - jobCtx, "java", "-jar", "/bin/fernflower.jar", "-mpm=30", job.inputPath, outputPathDir) + jobCtx, java, "-jar", "/bin/fernflower.jar", "-mpm=30", job.inputPath, outputPathDir) err := cmd.Run() if err != nil { log.V(5).Error(err, "failed to decompile file", "file", job.inputPath, job.outputPath) @@ -385,7 +387,7 @@ func explode(ctx context.Context, log logr.Logger, archivePath, projectPath stri artifactPath := filepath.Join(strings.Split(dep.ArtifactId, ".")...) destPath := filepath.Join(m2Repo, groupPath, artifactPath, dep.Version, filepath.Base(filePath)) - if err := copyFile(filePath, destPath); err != nil { + if err := CopyFile(filePath, destPath); err != nil { log.V(8).Error(err, "failed copying jar to m2 local repo") } else { log.V(8).Info("copied jar file", "src", filePath, "dest", destPath) @@ -433,7 +435,7 @@ func createJavaProject(ctx context.Context, dir string, dependencies []javaArtif } func moveFile(srcPath string, destPath string) error { - err := copyFile(srcPath, destPath) + err := CopyFile(srcPath, destPath) if err != nil { return err } @@ -444,7 +446,7 @@ func moveFile(srcPath string, destPath string) error { return nil } -func copyFile(srcPath string, destPath string) error { +func CopyFile(srcPath string, destPath string) error { if err := os.MkdirAll(filepath.Dir(destPath), 0755); err != nil { return err } @@ -465,6 +467,29 @@ func copyFile(srcPath string, destPath string) error { return nil } +func AppendToFile(src string, dst string) error { + // Read the contents of the source file + content, err := os.ReadFile(src) + if err != nil { + return fmt.Errorf("error reading source file: %s", err) + } + + // Open the destination file in append mode + destFile, err := os.OpenFile(dst, os.O_APPEND|os.O_WRONLY, 0644) + if err != nil { + return fmt.Errorf("error opening destination file: %s", err) + } + defer destFile.Close() + + // Append the content to the destination file + _, err = destFile.Write(content) + if err != nil { + return fmt.Errorf("error apending to destination file: %s", err) + } + + return nil +} + // toDependency returns javaArtifact constructed for a jar func toDependency(ctx context.Context, jarFile string) (javaArtifact, error) { // attempt to lookup java artifact in maven diff --git a/provider_container_settings.json b/provider_container_settings.json index 169a3b09..56c63780 100644 --- a/provider_container_settings.json +++ b/provider_container_settings.json @@ -101,6 +101,16 @@ "bundles": "/jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar" }, "analysisMode": "source-only" + }, + { + "location": "/analyzer-lsp/examples/gradle-multi-project-example", + "providerSpecificConfig": { + "lspServerName": "java", + "lspServerPath": "/jdtls/bin/jdtls", + "depOpenSourceLabelsFile": "/usr/local/etc/maven.default.index", + "bundles": "/jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar" + }, + "analysisMode": "source-only" } ] }, diff --git a/provider_local_external_images.json b/provider_local_external_images.json index e21c2f92..92a5129c 100644 --- a/provider_local_external_images.json +++ b/provider_local_external_images.json @@ -101,6 +101,16 @@ "bundles": "/jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar" }, "analysisMode": "source-only" + }, + { + "location": "examples/gradle-multi-project-example", + "providerSpecificConfig": { + "lspServerName": "java", + "lspServerPath": "/jdtls/bin/jdtls", + "depOpenSourceLabelsFile": "/usr/local/etc/maven.default.index", + "bundles": "/jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar" + }, + "analysisMode": "source-only" } ] }, diff --git a/provider_pod_local_settings.json b/provider_pod_local_settings.json index d62198a4..141257da 100644 --- a/provider_pod_local_settings.json +++ b/provider_pod_local_settings.json @@ -101,6 +101,16 @@ "bundles": "/jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar" }, "analysisMode": "source-only" + }, + { + "location": "/analyzer-lsp/examples/gradle-multi-project-example", + "providerSpecificConfig": { + "lspServerName": "java", + "lspServerPath": "/jdtls/bin/jdtls", + "depOpenSourceLabelsFile": "/usr/local/etc/maven.default.index", + "bundles": "/jdtls/java-analyzer-bundle/java-analyzer-bundle.core/target/java-analyzer-bundle.core-1.0.0-SNAPSHOT.jar" + }, + "analysisMode": "source-only" } ] }, diff --git a/rule-example.yaml b/rule-example.yaml index 4d614909..929d10ff 100644 --- a/rule-example.yaml +++ b/rule-example.yaml @@ -291,4 +291,12 @@ pattern: inclusion-test.xml - builtin.filecontent: pattern: "" - \ No newline at end of file +- category: mandatory + description: | + This rule looks for a class only present in the gradle project + effort: 3 + message: Only incidents in gradle project should appear + ruleID: java-gradle-project + when: + java.referenced: + pattern: com.sun.net.httpserver.HttpExchange