Git Product home page Git Product logo

Comments (8)

jjlauer avatar jjlauer commented on July 25, 2024

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 [email protected]
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
.

from rocker.

jjlauer avatar jjlauer commented on July 25, 2024

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 [email protected] 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 [email protected]
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
.

from rocker.

lethevimlet avatar lethevimlet commented on July 25, 2024

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.

from rocker.

jjlauer avatar jjlauer commented on July 25, 2024

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 [email protected]
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
.

from rocker.

lethevimlet avatar lethevimlet commented on July 25, 2024

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.

from rocker.

jjlauer avatar jjlauer commented on July 25, 2024

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?

from rocker.

lethevimlet avatar lethevimlet commented on July 25, 2024

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.

from rocker.

lethevimlet avatar lethevimlet commented on July 25, 2024

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.

from rocker.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.