GoogleCloudPlatform / functions-framework-java

FaaS (Function as a service) framework for writing portable Java functions
Apache License 2.0
133 stars 63 forks source link

java.lang.NoClassDefFoundError when adding a custom library to the classpath #224

Closed SButterfly closed 1 year ago

SButterfly commented 1 year ago

Hello.

Our team deploy Google function from a jar

gcloud functions deploy 'report-generation' --region=europe-west2 --entry-point  'my.company.ReportGenerationFunction' --source /target/deployment/

Once we decided to add our custom library to the dependencies, the function (in gcloud) started failing with

Failed to execute my.company.ReportGenerationFunction java.lang.NoClassDefFoundError: my/company/rules/CurrencyConversion

Locally everything works fine

Technical information: Deployment folder is generated like this

<build>
    <plugins>
      <plugin>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <classpathPrefix>libs/</classpathPrefix>
            </manifest>
          </archive>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <overWriteReleases>false</overWriteReleases>
              <includeScope>runtime</includeScope>
              <outputDirectory>${project.build.directory}/libs</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <executions>
          <execution>
            <id>copy-resources</id>
            <phase>package</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${project.build.directory}/deployment</outputDirectory>
              <resources>
                <resource>
                  <directory>${project.build.directory}</directory>
                  <includes>
                    <include>${project.build.finalName}.jar</include>
                    <include>libs/**</include>
                  </includes>
                  <filtering>false</filtering>
                </resource>
              </resources>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

Versions:

google.cloud.functions: 1.1.0
google.cloud.functions.plugin: 0.11.0
com.google.cloud:libraries-bom:26.21.0
garethgeorge commented 1 year ago

Hi @SButterfly, sorry you're running into a problem here! It looks like the entrypoint GCF is looking for is my/company/rules/CurrencyConversion which doesn't seem right based on your deploy command.

Let's start by verifying the configuration GCF has for your function, would you mind checking the entryPoint value output by gcloud functions describe <your function>?

SButterfly commented 1 year ago

Hi @garethgeorge! Turned out we had problems with the broken library + the company's maven repository. After fixing them the problem was gone.