Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Failed to discover main class" error when deploying a Spring Cloud Function #912

Open
jdubois opened this issue Nov 29, 2019 · 4 comments

Comments

@jdubois
Copy link
Contributor

jdubois commented Nov 29, 2019

The project I'm using here is the sample project I maintain as part of the official Azure Samples.

The current code, with the issue, is here: https://github.com/Azure-Samples/hello-spring-function-azure/tree/35d01ce2bed3b04d612ff56556f8298300905e90

This is related to ticket Azure-Samples/hello-spring-function-azure#1 on the Azure Samples project.

Plugin name and version

azure-functions-maven-plugin 1.3.2

Plugin configuration in your pom.xml

           <plugin>
                <groupId>com.microsoft.azure</groupId>
                <artifactId>azure-functions-maven-plugin</artifactId>
                <configuration>
                    <resourceGroup>${functionResourceGroup}</resourceGroup>
                    <appName>${functionAppName}</appName>
                    <region>${functionAppRegion}</region>
                    <appSettings>
                        <!-- Run Azure Function from package file by default -->
                        <property>
                            <name>WEBSITE_RUN_FROM_PACKAGE</name>
                            <value>1</value>
                        </property>
                        <property>
                            <name>FUNCTIONS_EXTENSION_VERSION</name>
                            <value>~2</value>
                        </property>
                        <property>
                            <name>FUNCTIONS_WORKER_RUNTIME</name>
                            <value>java</value>
                        </property>
                        <property>
                            <name>MAIN_CLASS</name>
                            <value>com.example.HelloFunction</value>
                        </property>
                    </appSettings>
                </configuration>
                <executions>
                    <execution>
                        <id>package-functions</id>
                        <goals>
                            <goal>package</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

Expected behavior

The main class of the function should be automatically found. The MAIN_CLASS property should not need to be specified in the pom.xml. Besides, as reported in Azure-Samples/hello-spring-function-azure#1 , this property is picked up correctly when using the "deploy" goal, but it doesn't work with the "run" goal.

Actual behavior

If we don't set up manually the MAIN_CLASS property, then we get the following error:

 java.lang.IllegalStateException: Failed to discover main class. An attempt was made to discover main class as 'MAIN_CLASS' environment variable, system property as well as entry in META-INF/MANIFEST.MF (in that order).

This fails both with the "deploy" and the "run" goals.

Steps to reproduce the problem

@Flanker32
Copy link
Member

@jdubois Thanks for your feedback and really sorry for the late reply. The error message shows that discover main class as 'MAIN_CLASS' environment variable, system property as well as entry in META-INF/MANIFEST.MF (in that order). However, jar packaged by spring-boot-maven-plugin which will use org.springframework.boot.loader.wrapper.ThinJarWrapper as the main class in MANIFEST according to your configuration, the real enterence com.example.HelloFunction will be set as Start-Class, so i think this may more likely an issue of function host, which should add new methods to search main class for spring cloud function.

As you said, we could add app setting MAIN_CLASS as a workaround, this will not work in local as maven plugin configuration will not be passed to function host in local, instead, please prepare a local.settings.json file in stagingDirectory folder(${PROJECT_FOLDER}\target\azure-functions\${FUNCTION_NAME},you may use maven-resources-plugin to do this job, link), here is an example of the file

{
    "IsEncrypted": false,
    "Values": {
      "AzureWebJobsStorage": "",
      "FUNCTIONS_WORKER_RUNTIME": "java",
      "MAIN_CLASS":"com.example.HelloFunction"
    }
  }

@jdubois
Copy link
Contributor Author

jdubois commented Dec 3, 2019

Oh thanks @Flanker32 !
I'm going to use your solution for my sample application, but it indeeds just looks like a workaround, and it would be better to solve it on the Function side - should I open another ticket there? Could you help me on this?

@Flanker32
Copy link
Member

Flanker32 commented Dec 4, 2019

@jdubois I have opened an issue in azure-functions-java-work to track this, here is the link

noce2 added a commit to math-dojo/user-account-service that referenced this issue Dec 11, 2019
This employs the workaround to the issue described
here: microsoft/azure-maven-plugins#912
@wangmingliang-ms wangmingliang-ms added this to the 2022-09 milestone Sep 5, 2022
@sergey124
Copy link

Now the library provides MAIN_CLASS to the Spring Cloud code if the field in local.settings.json is configured, see in Azure/azure-functions-java-worker#338 (comment)

Though it seems like overhead that we have to explicitly specify it in App Settings, even though it's added by mvn azure-functions:deploy automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants