So Forge installation is quite easy; however, creating your first Java EE 6 application will be even faster! Although we can create rather advanced applications with Forge, for the purpose of learning we will just use a simple schema that contains a user
table, which can be built using the following command:
CREATE TABLE user ( id int(10) NOT NULL PRIMARY KEY auto_increment, name varchar(50), surname varchar(50), email varchar(50));
The first thing we need to do is to create a new project using the new-project
command. Execute from within the Forge shell the following commands:
new-project --named forge-demo --topLevelPackage com.packtpub.as7development.appendix –projectFolder forge-demo
Now you have a new Forge project, which is based on a Maven project structure. Arguably, generating a new project isn't Forge's greatest value—the same could be achieved with Maven archetypes. The sweet part of Forge is that now you have the luxury of defining your own application skeleton interactively after it has already been generated. This means that you can create the project using the Maven archetype first, and then extend it using Forge's intuitive suggestions.
Since we will need to reverse engineer our database table into Java entities, we will install the hibernate-tools
plugin:
forge install-plugin hibernate-tools
The next step will be configuring the JPA layer for your application. This application will be based on JBoss' JPA implementation that is based on the Hibernate provider, referencing a database named MySQL
. This database is reachable at the JNDI named java:jboss/datasources/MySqlDS
.
persistence setup --provider HIBERNATE --container JBOSS_AS7 --database MYSQL --jndiDataSource java:jboss/datasources/MySqlDS
You will be asked to select which Java EE 6 POM you want to use for your application; you are advised to always use the latest stable version.
You can check the persistence.xml
file generated by Forge by using the cat
command:
[forge-demo] forge-demo $ cat C:forge-distribution-1.1.3.Finalinforge-demosrcmain esourcesMETA-INFpersistence.xml <?xml version="1.0" encoding="UTF-8" standalone="no"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="forge-default" transaction-type="JTA"> <description>Forge Persistence Unit</description> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>java:jboss/datasources/MySqlDS</jta-data-source> <exclude-unlisted-classes>false</exclude-unlisted-classes> <properties> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/> <property name="hibernate.transaction.flush_before_completion" value="true"/> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> </properties> </persistence-unit> </persistence>
Next, we will use the generate-entities
command (from the hibernate-tools
plugin) in order to generate your Entity
class. You will need to provide the following JDBC connection information:
You can specify all the parameters in a one-line command or complete it interactively as shown in the following transcript:
[forge-demo] forge-demo $ generate-entities ? Specify the URL for the JDBC connection. jdbc:mysql://localhost/ticketsystem ? Enter the user name for JDBC connection. [null] root ? Enter the password for JDBC connection. [ENTER for default] ***** ? Enter the dialect to use for the datasource. [org.hibernate.dialect.H2Dialect ] org.hibernate.dialect.MySQLDialect ? Specify the class name for the JDBC driver for the datasource. [org.h2.Driver ] com.mysql.jdbc.Driver ? Enter the path in the local file system to the jar file containing the JDBC driver. [null] C:forge-distribution-1.1.3.Finallibmysql-connector-java-5.1.18-bin.jar ? In which package you'd like to generate the entities, or enter for default: [com.packtpub.as7development.appendix.model] Found 1 tables in datasource Generated java at C:forge-distribution-1.1.3.Finalinforge-demosrcmainjavacompacktpubas7developmentappendixmodelUser.java Generated 1 java files.
After completing the persistence layer, we will now create the application GUI using the scaffold
command, which can be associated with several providers such as the JavaServer Faces provider:
[forge-demo] forge-demo $ scaffold setup --scaffoldType faces; ? Scaffold provider [faces] is not installed. Install it? [Y/n] Y ? 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] Y ***SUCCESS*** Installed [forge.maven.WebResourceFacet] successfully. ***SUCCESS*** Installed [forge.spec.ejb] successfully. ***SUCCESS*** Installed [forge.spec.cdi] successfully. ***SUCCESS*** Installed [forge.spec.servlet] successfully. ***SUCCESS*** Installed [forge.spec.jsf.api] successfully. ***SUCCESS*** Installed [faces] successfully. ? Create scaffold in which sub-directory of web-root? (e.g. http://localhost:8080/forge-demo/DIR) [/] view
As you can see from the previous transcript, the scaffold
command will need to update the project packaging format (from jar
to war
) and request a folder to use for storing scaffolding data.
Now we will generate a CRUD (create, read, update, and delete) view of your entities using the scaffold from-entity
command:
scaffold from-entity com.packtpub.as7development.appendix.model.* --overwrite; ***INFO*** Using currently installed scaffold [faces] ***SUCCESS*** Generated UI for [com.packtpub.as7development.appendix.model.User]
Please verify if the user's ID type matches with the corresponding UserBean ID type at the time of writing.
Now it is time to build your application using the build
command, which will compile and package your application in a web application archive (forge-demo.war
).
[forge-demo] forge-demo $ build [INFO] Scanning for projects... [INFO] [INFO] ---------------------------------------------------------- [INFO] Building forge-demo 1.0.0-SNAPSHOT [INFO] ---------------------------------------------------------- . . . . . . . . . . [INFO] Packaging webapp [INFO] Assembling webapp [forge-demo] in [C:forge-distribution-1.1.3.Finalinforge-demo argetforge-demo] [INFO] Processing war project [INFO] Copying webapp resources [C:forge-distribution-1.1.3.Finalinforge-demosrcmainwebapp] [INFO] Webapp assembled in [740 msecs] [INFO] Building war: C:forge-distribution-1.1.3.Finalinforge-demo argetforge-demo.war [INFO] --------------------------------------------------------- [INFO] BUILD SUCCESS [INFO] ---------------------------------------------------------- [INFO] Total time: 12.830s [INFO] Finished at: Sat Feb 23 19:59:47 CET 2013 [INFO] Final Memory: 7M/18M
The Maven build
command has been created for the artifact forge-demo.war
in the target
folder of your project. You can now either manually copy the archive into the deployments
folder of your application server (or alternatively use the management interfaces) or install the AS 7 Forge plugin by typing the following command:
[forge-demo] forge install-plugin jboss-as-7
Once you install the AS 7 plugin, you need to set up some configuration parameters (it is suggested that you leave the default values and just override the As7
folder, unless you want to let Forge download it for you).
[forge-demo] forge-demo $ as7 setup ? The Java Home 'C:Javajdk1.6.0_31' is already set, would you like to override it? [y/N] ? A default version of 7.1.3.Final is already set, would you like to override it? [y/N] ? Enter path for JBoss AS or leave blank to download: C:jboss-as-7.1.1.Final ***SUCCESS*** Installed [AS7ServerFacet] successfully.
Now you can deploy your application using the as7 deploy
command:
[forge-demo] forge-demo $ as7 deploy The deployment operation (FORCE_DEPLOY) was successful.
If, later on you want to undeploy the application from JBoss, you can use the corresponding command, which will remove your application from the available applications:
[forge-demo] forge-demo $ as7 undeploy
18.191.54.245