0

I'm trying to create a spring cloud function for GCP which will run as a background function. Ultimately I'll need to use database logic which is why there's entity and h2, but at the moment the repos, entities, etc do not exist. I have two classes, one with the main method and the other with the task. All it's trying to do is print the string it was given, but it's having trouble starting with an error I've never seen before.

Command: mvn function:run

pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.6</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yuknis</groupId>
    <artifactId>in</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>in</name>
    <description>Receive a message and perform any transformation on it</description>
    <properties>
        <java.version>17</java.version>
        <spring-cloud.version>2022.0.2</spring-cloud.version>
        <spring-cloud-function.version>4.0.3-SNAPSHOT</spring-cloud-function.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-function-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-function-adapter-gcp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>8.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <!-- <scope>test</scope> -->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <outputDirectory>target/deploy</outputDirectory>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework.cloud</groupId>
                        <artifactId>spring-cloud-function-adapter-gcp</artifactId>
                        <version>${spring-cloud-function.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>com.google.cloud.functions</groupId>
                <artifactId>function-maven-plugin</artifactId>
                <version>0.9.1</version>
                <configuration>
                    <functionTarget>org.springframework.cloud.function.adapter.gcp.GcfJarLauncher</functionTarget>
                    <port>8080</port>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.5.0</version>
                <configuration>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.5.0</version>
                <configuration>
                </configuration>
            </plugin>
        </plugins>
    </reporting>
</project>

application.yaml

spring:
  config.activate.on-profile: default
  datasource:
    url: jdbc:h2:mem:mydb
    username: redacted
    password: redacted
    driverClassName: org.h2.Driver
  data.jdbc.repositories.enabled: false
  jpa:
    spring.jpa.database-platform: org.hibernate.dialect.H2Dialect
    hibernate:
      ddl-auto: update
    show-sql: true
    properites:
      hibernate:
        format_sql: true

resources/META-INF/MANIFEST.MF

Main-Class: com.yuknis.in.InApplication

InApplication.java

package com.yuknis.in;

import java.util.function.Consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import com.yuknis.in.task.ProcessFileTask;

/**
 * Receives a message and performs any transformation on it.
 */
@SpringBootApplication
public class InApplication {

    /**
     * The main entry point of the application.
     * 
     * @param args Command line arguments passed to the application.
     */
    public static void main(String[] args) {

        SpringApplication.run(InApplication.class, args);

    }

    /**
     * Provides the Java Function which contains the code for the task.
     * 
     * @return The function containing the logic needed to acomplish the task.
     */
    @Bean
    Consumer<String> executeTask() {

        return new ProcessFileTask();

    }

}

ProcessFileTask.java

package com.yuknis.in.task;

import java.util.function.Consumer;

/**
 * The function which is used to process the incomming message when the task is triggered.
 */
public class ProcessFileTask implements Consumer<String> {

    /**
     * Accepts a message and commits it's contens to the database.
     * 
     */
    @Override
    public void accept(String t) {
        
        System.out.println(String.format("%s", t));

    }
    
}

And finally the error is: [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.413 s [INFO] Finished at: 2023-05-05T10:59:26-04:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal com.google.cloud.functions:function-maven-plugin:0.9.1:run (default-cli) on project in: Could not invoke function: Could not construct an instance of org.springframework.cloud.function.adapter.gcp.GcfJarLauncher: java.lang.reflect.InvocationTargetException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: net/bytebuddy/NamingStrategy$SuffixingRandom$BaseNameResolver: net.bytebuddy.NamingStrategy$SuffixingRandom$BaseNameResolver -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Kirkland
  • 798
  • 1
  • 8
  • 20
  • I commented out one library at a time and found out it goes away when I comment out `spring-boot-starter-data-jpa`. We need that package though? – Kirkland May 05 '23 at 17:36
  • I've tried all solutions labeled here, nothing worked: https://stackoverflow.com/questions/51384547/facing-hibernate-error-caused-by-java-lang-classnotfoundexception-net-bytebud – Kirkland May 05 '23 at 19:56

0 Answers0