問題描述
是否有類似 JET(Java Emitter Templates)但沒有 Eclipse 依賴項的東西? (Is there something like JET (Java Emitter Templates) but without Eclipse‑dependencies?)
I wanted to use JET (Java Emitter Templates) in my Netbeans projects, but had to find out that JET heavily depends on Eclipse libraries.
Is there something similar to JET, but as a standalone project? Something which is open source and well maintained?
Futhermore, is "code generation" the common term for such tools?
‑‑‑‑‑
參考解法
方法 1:
If you are using Maven, you can use JET templates with the maven‑jet‑plugin.
This seems to be the source code. And here the documentation.
It is not actively maintained but works pretty well and follows the JET spec. I've used it with templates and skeletons.
It's self contained, doesn't depend on Eclipse, and doesn't introduce any transitive dependencies to your project.
方法 2:
Indeed JET is very tied with Eclipse.
If you want to generate code without Eclipse you should use another solution.
You can choose another template engine like Velocity (https://velocity.apache.org/) or FreeMarker (https://freemarker.apache.org/).
Or you can choose a code generator working by itself independently of any IDE.
For example "Telosys Command Line Interface" : http://www.telosys.org/
方法 3:
From what I know, JET is something like JSP, no?
Java Emitter Templates are very similar to Java Server Pages (JSPs). Both JETs and JSPs use the same syntax, and are compiled to Java behind the scenes. Both are used to separate the responsibility for rendering pages from the model and controller. Both accept objects passed into them as an input argument, both allow inserting string values within code ("expressions"), and allow direct use of Java code to perform loops, declare variable, or perform logical flows ("scriptlets"). Both are good ways of representing the structure of a generated object (web page, Java class, or file) while supporting customization of the details. JETs differ from JSPs in a few key ways. In a JET, the structure of the markup may be changed to support generating code in different languages. Typically the input to a JET will be a configuration file and not user input (though there is nothing forbidding this). And also typically, JET processing will take place only once for a given workflow. These are not technical limitations, and you may find uses for JETs which are quite different... ibm.com
Here are a few links to get you started on JSP, if that sounds like what you need: sun.com netbeans.org
方法 4:
Look for "template engine" for these types of tools.
A couple you might want to look at:
- Apache Velocity (http://velocity.apache.org/)
- StringTemplate (http://stringtemplate.org/)
方法 5:
I ended up using ERB (Ruby's template engine).
Works great in Netbeans!
I define custom ant task which generates source files by calling ERB (whose results are placed inside a non‑versioned special directory).
The ant task is overriding Netbeans' "‑pre‑compile" task.
(by ivan_ivanovich_ivanoff、Daniel Sperry、lgu、Kirill Strizhak、Scott Stanchfield、ivan_ivanovich_ivanoff)