Writing Basic Java EE Application
For the most part, people interested in Forge are likely interested in creating web-applications. Thusly, this chapter will overview the basic steps to generate such an application using Forge.
Get Started with Scaffolding
Assuming you have already completed the steps to install Forge, the first thing you’ll need to do is download and install a Java EE application server, for example JBoss Application Server 7. This server will host your application once it is built.
Next, follow these steps to create your skeleton web-application; be sure to replace any {ARGS} with your own personal values. Also keep in mind that while typing commands, you may press
Start Forge
Execute forge
from a command prompt, or open your shortcut, if you created one during the installation process.
Create a new project
$ new-project --named {name} --topLevelPackage {com.example.project} --projectFolder {/directory/path}
Set up scaffolding
Press ENTER to confirm installation of any required facet dependencies and/or packaging types. You should get these 3 questions:
$ scaffold setup
? No scaffold type was selected, use default [JavaServer Faces]? [Y/n]
? Scaffold provider [faces] is not installed. Install it? [Y/n]
? Facet [forge.maven.WebResourceFacet] requires packaging type(s) [war], but is currently [jar]. Update packaging? (Note: this could deactivate other plugins in your project.) [Y/n]
Press ENTER for those 3 questions. Next, we have:
Use which version of 'jboss-javaee-6.0' ?
Again, press ENTER for the default value:
? Choose an option by typing the number of the selection [*-default]
Keep on pressing ENTER to choose default values for the next questions:
? Create scaffold in which sub-directory of web-root? (e.g. http://localhost:8080/ForgeTest/DIR) [/]
Set up persistence (JPA)
Press ENTER to confirm installation of any required facet dependencies and/or packaging types, and remember to press TAB if you are not sure what comes next.
$ persistence setup --provider {your JPA implementation} --container {your container}
If you choose a Java EE container, press ENTER to answer default values for the next questions:
? Do you want to install a JPA 2 metamodel generator? [y/N]
If you choose HIBERNATE:
? The JPA provider [HIBERNATE], also supplies extended APIs. Install these as well? [y/N]
If you do not wish to use a Java EE container default data-source, you can also specify additional connection parameters such as JNDI data-source names, JDBC connection information, and data-source types. Note, however, that this means you will probably need configure your application server to provide this new data-source and/or database connection.
Create some JPA entities:
$ entity --named Customer
At which point Forge will automatically pick-up the newly created entity, and will be ready to add fields. We refer to this as “holding” an entity, or “holding” a file.
Customer.java $ field string --named firstName
Customer.java $ field string --named lastName
While “holding” most files, you may inspect them using ‘ls’.
Customer.java $ ls
[fields] private::String::firstName; private::String::lastName; private::int::version; private::long::id;
[methods] public::getFirstName()::String public::getId()::long public::getLastName()::String public::getVersion()::int public::setFirstName(final String firstName)::void public::setId(final long id)::void public::setLastName(final String lastName)::void public::setVersion(final int version)::void public::toString()::String
Customer.java $
Picking up other files
As stated above, creating a new entity will automatically “pick-up” and “hold” that file. When a file is “held”, that file is the context that operations are performed in. To change that context and “pickup” another file, use the pick-up
command.
Eg. $ pick-up src/main/java/com/company/Client.java
Generate some UI scaffolding!
Once you have created fields in the entity, it’s time to generate some scaffolding. Since we have already installed our scaffold, this step is easy. While “holding” Customer.java, type:
Customer.java $ scaffold from-entity
? No scaffold type was selected, use default (JSF)? [Y/n] Wrote /src/main/java/com/scaffold/domain/Customer.java Wrote /src/main/java/com/scaffold/view/CustomerBean.java Wrote /src/main/webapp/scaffold/customer/view.xhtml Wrote /src/main/webapp/scaffold/customer/create.xhtml Wrote /src/main/webapp/scaffold/customer/list.xhtml ***SUCCESS*** Generated UI for [com.scaffold.domain.Customer]
Customer.java $
That’s it! Now build your project and deploy it onto your Java EE Application Server of choice:
$ build
Or, to control the build more finely, you can also build your new project using native Maven commands. (If you do not have maven installed, Forge will use its provided embedded Maven support.)
$ mvn clean package
(Optional) Deploy/undeploy to AS7
First you need to install the AS7 Forge Plugin by typing the following command:
$ forge install-plugin jboss-as-7
Next, set up the plugin in your project:
$ as7 setup
Then you will be able to deploy, redeploy, and un-deploy as desired using the following commands:
$ as7 deploy
$ as7 redeploy
$ as7 undeploy
That’s it! Access your deployed application at:
http://localhost:8080/projectname