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

Generate CLI app with correct main class name (was: CLI command stopped working when app is assembled) #474

Closed
sascha-frinken opened this issue Aug 6, 2018 · 32 comments
Assignees
Milestone

Comments

@sascha-frinken
Copy link
Contributor

sascha-frinken commented Aug 6, 2018

I am currently playing around with the CLI profile (picocli) on latest snapshot. The app stopped working when assembled and called via java -jar …. Running the app as Application from my IntelliJ IDE still works.

Steps to Reproduce

  • create an app with profile cli enabled
  • create a command
  • assemble app
  • call jar
$ mn create-app my-cli-app --profile cli
Generating Java project...
| Application created at /tmp/my-cli-app
$ cd my-cli-app/
$ mn
| Starting interactive mode...
| Enter a command name to run. Use TAB for completion:
mn> create-command test
| Rendered template Command.java to destination src/main/java/my/cli/app/TestCommand.java
| Rendered template CommandTest.java to destination src/test/java/my/cli/app/TestCommandTest.java

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.8/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 4s
11 actionable tasks: 10 executed, 1 up-to-date
$ java -jar build/libs/my-cli-app-0.1-all.jar -v
11:14:05.005 [main] INFO  io.micronaut.runtime.Micronaut - No embedded container found. Running as CLI application

Expected Behaviour

CLI command is called and shows Hi!

Actual Behaviour

CLI command is not invoked instead the message:
INFO io.micronaut.runtime.Micronaut - No embedded container found. Running as CLI application shows up

Environment Information

  • Operating System: Linux 4.17.11-200.fc28.x86_64
  • Micronaut Version: 1.0.0.BUILD-SNAPSHOT
  • JDK Version: 1.8.0_171

Example Application

n/a

@graemerocher
Copy link
Contributor

@remkop Any ideas on this one?

@remkop
Copy link
Contributor

remkop commented Aug 7, 2018

Yes, I believe this is because the main class in the build.gradle file in the generated application is still pointing to Application.

Before building the project, open the gradle.build file, and change the mainClassName line to:

mainClassName = "my.cli.app.TestCommand"

Now, when you rebuild the project and start the application, it should work as expected.

FYI, there is work in progress to improve this so that the build.gradle for a generated CLI app has the correct main class name. Hopefully this can make it into the next micronaut release. (@graemerocher is there a timeline?)

@graemerocher
Copy link
Contributor

Thursday we will do the next release

@remkop
Copy link
Contributor

remkop commented Aug 7, 2018

ok. I'll hurry. :-)

@sascha-frinken
Copy link
Contributor Author

Thank you guys!
Just let me know if you want me to close the issue.

@remkop
Copy link
Contributor

remkop commented Aug 7, 2018

First: did that solve the issue?

@sascha-frinken
Copy link
Contributor Author

Of course.

@remkop
Copy link
Contributor

remkop commented Aug 7, 2018

Let's leave it open. I will rename this issue and reference it from my PR for creating CLI apps with the correct main class set.

@remkop
Copy link
Contributor

remkop commented Aug 7, 2018

Oops, I cannot rename it. Anyway, I'll reference it from the PR.

@sascha-frinken
Copy link
Contributor Author

Just let me know how you want the issue to be named and I'll do that for you.

@remkop
Copy link
Contributor

remkop commented Aug 7, 2018

How about "Generate CLI app with correct main class name (was: CLI command stopped working when app is assembled)" ?

@sascha-frinken sascha-frinken changed the title CLI command stopped working when app is assembled Generate CLI app with correct main class name (was: CLI command stopped working when app is assembled) Aug 7, 2018
@remkop
Copy link
Contributor

remkop commented Aug 7, 2018

Thank you!

@remkop
Copy link
Contributor

remkop commented Aug 8, 2018

@graemerocher Status update: implementation and testing done; todo: update documentation. I hope to push be able to this tonight.

@remkop
Copy link
Contributor

remkop commented Aug 8, 2018

@graemerocher it took a while, but please find PR #480 and PR micronaut-projects/micronaut-profiles#59 that address this issue.

graemerocher added a commit that referenced this issue Aug 8, 2018
@graemerocher graemerocher added this to the 1.0.0-M4 milestone Aug 8, 2018
@graemerocher
Copy link
Contributor

Merged. Thanks

@remkop
Copy link
Contributor

remkop commented Aug 8, 2018

@graemerocher That was fast! :-) Did you also merge the accompanying micronaut-profiles PR micronaut-projects/micronaut-profiles#59 ? (They need to go together)

@remkop
Copy link
Contributor

remkop commented Aug 8, 2018

I now see both PRs are merged. Thanks!

@ghost
Copy link

ghost commented Sep 24, 2018

Any solution for same issue but in maven project?

My dependencies are the following:

    <!-- JAVA 9+ and LOG dependencies -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-jdk14</artifactId>
        <version>1.7.25</version>
    </dependency>
    <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>1.3.2</version>
    </dependency>

    <!-- Project dependencies -->
    <dependency>
        <groupId>io.micronaut</groupId>
        <artifactId>runtime</artifactId>
        <version>1.0.0.M4</version>
    </dependency>
    <dependency>
        <groupId>io.micronaut</groupId>
        <artifactId>http-client</artifactId>
        <version>1.0.0.M4</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>

@remkop
Copy link
Contributor

remkop commented Sep 24, 2018

@mOo7King Did you generate the app with the create-cli-app command?

@remkop
Copy link
Contributor

remkop commented Sep 24, 2018

@mOo7King The user manual has more details: https://docs.micronaut.io/latest/guide/index.html#picocliGenerateProject

@ghost
Copy link

ghost commented Sep 24, 2018

I followed this (http://guides.micronaut.io/creating-your-first-micronaut-app/guide/index.html) guide but instead of generate a Gradle project I created a new project with that maven dependencies I said before.
In that way I only have two classes: Application.java and HelloController.java.
My purpose was to simply run Application.java in Intellij's IDE and what I expected was the creation of a random port localhost which I would be able to invoke with, for example, postman's application.
What really happens is that when I run the class the following message appears:

"io.micronaut.runtime.Micronaut start
INFO: No embedded container found. Running as CLI application"


HelloController.java

import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.Produces;

@Controller("/hello") 
public class HelloController {
    @Get("/") 
    @Produces(MediaType.TEXT_PLAIN) 
    public String index() {
        return "Hello World"; 
    }
}

Application.java

import io.micronaut.runtime.Micronaut;

  public class Application {
    public static void main(String[] args) {
        Micronaut.run(Application.class);
    }
 }

pom.xml

<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 http://maven.apache.org/xsd/maven- 
         4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.microservice</groupId>
    <artifactId>example-micronaut</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>

        <!-- JAVA 9+ and LOG dependencies -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-jdk14</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.2</version>
        </dependency>

        <!-- Project dependencies -->
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>runtime</artifactId>
            <version>1.0.0.M4</version>
        </dependency>
        <dependency>
            <groupId>io.micronaut</groupId>
            <artifactId>http-client</artifactId>
            <version>1.0.0.M4</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

</project>

@remkop
Copy link
Contributor

remkop commented Sep 26, 2018

@mOo7King Is this still an issue?

@ghost
Copy link

ghost commented Sep 26, 2018

@remkop I decided to generate a Gradle project just like the guide and it worked perfectly. However I need to convert that Gradle project into a Maven one (requirement I need to reach) and as a newbie I don't know how to do that. Any help would be appreciated.

@remkop
Copy link
Contributor

remkop commented Sep 26, 2018

@mOo7King I missed your earlier comment. I understand a bit better now. I believe you are commenting on the wrong ticket. This ticket is about creating a command line Micronaut application that uses picocli for command line parsing (so the application can have command line options, subcommands etc.)

What you are looking to do does not require command line parsing.

I am not 100% sure why you are seeing this error

io.micronaut.runtime.Micronaut start INFO: No embedded container found. Running as CLI application"

...but it seems that you may be missing a dependency in your POM.
The best way to proceed I think is to either raise a separate ticket where you re-state the problem (the comment that starts with "I followed this (http://guides.micronaut.io/creating-your-first-micronaut-app/guide/index.html) guide but ..." is a good text for such a ticket), or start a conversation on https://gitter.im/micronautfw/

That way people who are more expert than me can help out.

@ghost
Copy link

ghost commented Sep 26, 2018

@remkop Thank you. I started a new issue: #630

@gavinws
Copy link

gavinws commented Oct 26, 2018

I hate to bring up an old issue, but I am seeing the same problem happen with the --build maven option. This is my first time using Micronaut or PicoCLI. I can see that your changes were not undone after your PRs were merged, so I'm not sure what's up with this unless Micronaut 1.0.0 was built on the wrong changeset or something.

What I did on macOS High Sierra (10.13.6) with Azul Zulu JDK 8 (Java 8u162):

  1. Installed SDKMan
  2. Installed Micronaut 1.0.0
  3. mn create-cli-app --build maven analyzer
  4. mvn clean install
  5. java -jar target/analyzer-0.1.jar -v (also used -h and --verbose)

The output is:

13:51:15.915 [main] INFO  io.micronaut.runtime.Micronaut - No embedded container found. Running as CLI application

Checking the pom.xml I see:

  <properties>
    <exec.mainClass>analyzer.Application</exec.mainClass>
    <micronaut.version>1.0.0</micronaut.version>
    <jdk.version>1.8</jdk.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

After changing mainClass to <exec.mainClass>analyzer.AnalyzerCommand</exec.mainClass> and rebuilding, I get the expected output when running it.

@remkop
Copy link
Contributor

remkop commented Oct 27, 2018

I’ll try if I can reproduce the issue.

@remkop
Copy link
Contributor

remkop commented Oct 27, 2018

I've been able to reproduce the issue: it works for Gradle but is broken for Maven: the main class is analyzer.Application in Maven.
@graemerocher Is there some difference in how the tokens in the skeletons are replaced?...

@remkop
Copy link
Contributor

remkop commented Oct 27, 2018

Looking further, in the micronaut-profiles project, both cli/skeleton/gradle-build/build.gradle and cli/skeleton/maven-build/pom.xml are using the same tokens for the main class: @defaultPackage@.@project.className@Command. This looks correct, but somehow only the resulting Gradle build file has the correct command class as the main class while the resulting Maven POM has the default Application class as main.

The cli profile extends the base profile, and the base/skeleton/maven-build/pom.xml has this section:

<!-- base/skeleton/maven-build/pom.xml -->
<properties>
    <exec.mainClass>@defaultPackage@.Application</exec.mainClass>
    <micronaut.version>@version@</micronaut.version>
    <jdk.version>@jdkversion@</jdk.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

I am guessing that this prevents the definition of the tokens in cli/skeleton/maven-build/pom.xml to take effect...

<!-- cli/skeleton/maven-build/pom.xml : this is not taking effect -->
<properties>
    <exec.mainClass>@defaultPackage@.@project.className@Command</exec.mainClass>
</properties>

The base/skeleton/gradle-build/build.gradle does not define a main class property, so there is no possibility of a conflict with the definition in the cli build file.

Still, I'm puzzled, because specifying a different main class does seem to work in function-aws/features/function-aws-java/skeleton/maven-build/pom.xml...

@ZacharyKlein any idea what could be happening here?

@ZacharyKlein
Copy link
Contributor

Limited internet connectivity at the moment, but I will look into this early next week and get back to you.

@ZacharyKlein ZacharyKlein reopened this Oct 27, 2018
@jameskleeh jameskleeh self-assigned this Nov 1, 2018
@jameskleeh jameskleeh modified the milestones: 1.0.0-M4, 1.0.1 Nov 1, 2018
@jameskleeh
Copy link
Contributor

Fixed by micronaut-projects/micronaut-profiles@2278087

@remkop
Copy link
Contributor

remkop commented Nov 1, 2018

Thanks @jameskleeh !

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

No branches or pull requests

6 participants