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

Compiled classes have missing package declaration. #37

Closed
lethevimlet opened this issue Sep 20, 2016 · 8 comments
Closed

Compiled classes have missing package declaration. #37

lethevimlet opened this issue Sep 20, 2016 · 8 comments

Comments

@lethevimlet
Copy link

Compiled templates create java files that lacks the package declaration on the first line, this causes issues on several IDE's and compile environments.

At least a property to enable package inclusion/exclusion should be added to fit all project needs.

// <-- Package with the same path as outputDirectory should go here!!!

import java.io.IOException;
import com.fizzed.rocker.ForIterator;
import com.fizzed.rocker.RenderingException;
import com.fizzed.rocker.RockerContent;
import com.fizzed.rocker.RockerOutput;
import com.fizzed.rocker.runtime.DefaultRockerTemplate;
import com.fizzed.rocker.runtime.PlainTextUnloadedClassLoader;

/*
 * Auto generated code to render template /TestTempalte.rocker.html
 * Do not edit this file. Changes will eventually be overwritten by Rocker parser!
 */
public class TestTempalte extends com.fizzed.rocker.runtime.DefaultRockerModel {

...
@jjlauer
Copy link
Member

jjlauer commented Sep 20, 2016

No, rocker definitely creates package names. You are most definitely
missing a property the generator process resulting in no package
declaration. There are 3 properties that must be set for Rocker to figure
out what you meant as the package name.

You should set output directory & template directory.

-Joe

On Tue, Sep 20, 2016 at 12:51 PM, joselightware notifications@github.com
wrote:

Compiled templates create java files that lacks the package declaration on
the first line, this causes issues on several IDE's and compile
environments.

At least a property to enable package inclusion/exclusion should be added
to fit all project needs.

// <-- Package with the same path as outputDirectory should go here!!!

import java.io.IOException;
import com.fizzed.rocker.ForIterator;
import com.fizzed.rocker.RenderingException;
import com.fizzed.rocker.RockerContent;
import com.fizzed.rocker.RockerOutput;
import com.fizzed.rocker.runtime.DefaultRockerTemplate;
import com.fizzed.rocker.runtime.PlainTextUnloadedClassLoader;

/*

  • Auto generated code to render template /TestTempalte.rocker.html
  • Do not edit this file. Changes will eventually be overwritten by Rocker parser!
    */
    public class TestTempalte extends com.fizzed.rocker.runtime.DefaultRockerModel {

...


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#37, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAjwAlmQD_lSu9Xgc-Q6-wdAtge-ShXwks5qsA8LgaJpZM4KB3Pt
.

@jjlauer
Copy link
Member

jjlauer commented Sep 20, 2016

If you send me your project structure and what you're setting the config to
I can help pinpoint it. If its like your answer to gradle, then what are
you putting your views in?

E.g. src/main/resources --> the package name detection would start from
inside resources. So you'd need to put your template in
src/main/resources/views/my.rocker.html for a package of "views" to be
calculated.

On Tue, Sep 20, 2016 at 1:17 PM, Joe Lauer joe@lauer.bz wrote:

No, rocker definitely creates package names. You are most definitely
missing a property the generator process resulting in no package
declaration. There are 3 properties that must be set for Rocker to figure
out what you meant as the package name.

You should set output directory & template directory.

-Joe

On Tue, Sep 20, 2016 at 12:51 PM, joselightware notifications@github.com
wrote:

Compiled templates create java files that lacks the package declaration
on the first line, this causes issues on several IDE's and compile
environments.

At least a property to enable package inclusion/exclusion should be added
to fit all project needs.

// <-- Package with the same path as outputDirectory should go here!!!

import java.io.IOException;
import com.fizzed.rocker.ForIterator;
import com.fizzed.rocker.RenderingException;
import com.fizzed.rocker.RockerContent;
import com.fizzed.rocker.RockerOutput;
import com.fizzed.rocker.runtime.DefaultRockerTemplate;
import com.fizzed.rocker.runtime.PlainTextUnloadedClassLoader;

/*

  • Auto generated code to render template /TestTempalte.rocker.html
  • Do not edit this file. Changes will eventually be overwritten by Rocker parser!
    */
    public class TestTempalte extends com.fizzed.rocker.runtime.DefaultRockerModel {

...


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#37, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAjwAlmQD_lSu9Xgc-Q6-wdAtge-ShXwks5qsA8LgaJpZM4KB3Pt
.

@lethevimlet
Copy link
Author

lethevimlet commented Sep 20, 2016

Thanks for your quick reply, yes indeed I'm using gradle although in a different way as explained on my gradle post.

Here is what I'm doing:

  1. I have custom gradle plugin which is an independent jar, this file contains a method that uses your generator to compile the templates. This method is exposed as a gradle task to any project using the plugin.
public static void compileRockerTemplates(String rockerTemplateDirectory, String rockerOutputDirectory, String rockerClassDirectory) {
    try {

      JavaGeneratorMain jgm = new JavaGeneratorMain();

      File templateDirectory = new File(rockerTemplateDirectory);
      File outputDirectory = new File(rockerOutputDirectory);
      File classDirectory = new File(rockerClassDirectory);

      jgm.getParser().getConfiguration().setTemplateDirectory(templateDirectory);
      jgm.getParser().getConfiguration().setOutputDirectory(outputDirectory);
      jgm.getGenerator().getConfiguration().setClassDirectory(classDirectory);

      jgm.setFailOnError(true);

      jgm.run();

    } catch (Exception ex) {
      Logger.getLogger(Extras.class.getName()).log(Level.SEVERE, null, ex);
    }

  1. I use the plugin in my main project with the following config that will get passed to the compileRockerTemplates method.
rockerTemplateDirectory = project.projectDir.toString() + '/src/main/java/com/joe/view/template'
rockerOutputDirectory = project.projectDir.toString() + '/src/main/java/com/joe/view/template'
rockerClassDirectory = project.projectDir.toString() + '/src/main/java/com/joe/view/template'

with the following template inside the template folder

@args (String world)

<!DOCTYPE html>
<html>
  <head>
    <title>TODO supply a title</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  </head>
  <body>
    <div>@world</div>
  </body>
</html>

When I call my compileRockerTemplates task templates get compiled as expected

image

  1. The template is finally rendered inside a JAXRS method to expose it to the web client.
@Stateless
@Path("test")
public class TestVIEW {

  @GET
  public String testView(){
    return TestTemplate.template("Hello World!")
    .render()
    .toString();
  }

}

The only problem is that the generated TestTemplate.java lacks the package declaration, if I manually add it everything works as expected, I was considering to add it myself on the compileRockerTemplates method modifying the files but maby as you point out I'm missing something that I should intialize on JavaGeneratorMain

Many thanks for your help and advise.

@jjlauer
Copy link
Member

jjlauer commented Sep 20, 2016

Simple fix. The base template directory is base dir where the package is
calculated from. If you wanted your final package to be "view.template"
then you'd just need to change the settings to:

rockerTemplateDirectory = project.projectDir.toString() +
'/src/main/java/com/joe'
rockerOutputDirectory = project.projectDir.toString() + '/src/main/java/com/joe'
rockerClassDirectory = project.projectDir.toString() + '/src/main/java/joe'

Or to be a java package of 'com.joe.view.template' then:

rockerTemplateDirectory = project.projectDir.toString() + '/src/main/java'
rockerOutputDirectory = project.projectDir.toString() + '/src/main/java'
rockerClassDirectory = project.projectDir.toString() + '/src/main/java'

However, I'm not familiar with gradle, but I'm guessing there's a
better standard directory to put your generated sources? Polluting
your src/main/java with them may not be the best practice. In maven,
the default directory is something like
target/generated-sources/rocker. Most IDEs also pick up that
directory so code completion, etc. works.

The class directory is definitely wrong. That should be set to the
place your .java files are compiled to. Again in maven, that would be
something like target/classes.

On Tue, Sep 20, 2016 at 2:15 PM, joselightware notifications@github.com
wrote:

Thanks for your quick reply, yes indeed I'm using gradle although in a
different way as explained on my gradle post.

Here is what I'm doing:

  1. I have custom gradle plugin which is a independent jar, this file
    contains a method that uses your generator to compile the templates. this
    method is exposed as a gradle task to any project using the plugin.

public static void compileRockerTemplates(String rockerTemplateDirectory, String rockerOutputDirectory, String rockerClassDirectory) {
try {

  JavaGeneratorMain jgm = new JavaGeneratorMain();

  File templateDirectory = new File(rockerTemplateDirectory);
  File outputDirectory = new File(rockerOutputDirectory);
  File classDirectory = new File(rockerClassDirectory);

  jgm.getParser().getConfiguration().setTemplateDirectory(templateDirectory);
  jgm.getParser().getConfiguration().setOutputDirectory(outputDirectory);
  jgm.getGenerator().getConfiguration().setClassDirectory(classDirectory);

  jgm.setFailOnError(true);

  jgm.run();

} catch (Exception ex) {
  Logger.getLogger(Extras.class.getName()).log(Level.SEVERE, null, ex);
}
  1. I use the plugin in my main project with the following config that will
    get passed to the compileRockerTemplates method.

rockerTemplateDirectory = project.projectDir.toString() + '/src/main/java/com/joe/view/template'
rockerOutputDirectory = project.projectDir.toString() + '/src/main/java/com/joe/view/template'
rockerClassDirectory = project.projectDir.toString() + '/src/main/java/joe/view/template'

with the following template inside the template folder

@Args (String world)

<title>TODO supply a title</title>

When I call my compileRockerTemplates task templates get compiled as
expected
[image: image]
https://cloud.githubusercontent.com/assets/2963923/18682838/0bad3a8c-7f6e-11e6-8339-6b0c02c11de7.png

  1. The template is finally rendered inside a JAXRS method to expose it to
    the web client.

@stateless
@path("test")
public class TestVIEW {

@get
public String testView(){
return TestTemplate.template("Hello World!")
.render()
.toString();
}

}

The only problem is that the generated TestTemplate.java lacks the package
declaration, if I manually add it everything works as expected, I was
considering to add it myself on the compileRockerTemplates method modifying
the files but maby as you point out I'm missing something that I should
intialize on JavaGeneratorMain

Many thanks for your help and advise.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#37 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAjwAvdmJUyPJFNOjHaaaKtVg8K76uwbks5qsCLJgaJpZM4KB3Pt
.

@lethevimlet
Copy link
Author

lethevimlet commented Sep 20, 2016

Nice, I solved the issue with your advise.
Testing with the paths, I found that I can keep the templates inside the "src/main/resources/" plus the same structure where i want the files to be output on "src/main/java"

so in my case i'll place the templates on "src/main/resources/com/jo"
so that the java files output to "src/main/java/com/jo"
with the correct package

I achive this with the following configuration.

 rockerTemplateDirectory = project.projectDir.toString() + '/src/main/resources'
 rockerOutputDirectory = project.projectDir.toString() + '/src/main/java'
 rockerClassDirectory = project.projectDir.toString()

I placed the rocker-compiler.conf on the project root since I'm on a web project and theres no target, the only option to place it where compiled classes are would be to place it on "src/main/webapp/WEB-INF" but I'm not sure why is the rocker-compiler.conf file needed on a web project.

Thank you very much for your help, this issue is closed for me, hope it helps other gradlers out there.

@jjlauer
Copy link
Member

jjlauer commented Sep 20, 2016

Great. Sounds like a nice solution. Any interest in submitting a PR for the README.md that shows how to use Rocker generator from Gradle?

@jjlauer jjlauer closed this as completed Sep 20, 2016
@lethevimlet
Copy link
Author

Sure if not a PR ill PM you the code ^_^, just need to fix the other issue#36 with BindingRockerModel to make sure everything is working.

@lethevimlet
Copy link
Author

I've uploaded a working rocker build.gradle to issue#33 with a downloadable example project, so this can serve as groundwork for a future gralde-rocker-plugin and readme documentation.

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

2 participants