{"id":1093,"date":"2010-08-01T11:28:53","date_gmt":"2010-08-01T16:28:53","guid":{"rendered":"http:\/\/www.andygibson.net\/blog\/?p=1093"},"modified":"2010-08-13T09:28:52","modified_gmt":"2010-08-13T14:28:52","slug":"knappsack-archetypes-part-1","status":"publish","type":"post","link":"http:\/\/www.andygibson.net\/blog\/article\/knappsack-archetypes-part-1\/","title":{"rendered":"Knappsack Archetypes Part 1"},"content":{"rendered":"<p>This set of articles will document the contents of the Java EE archetypes for Maven. The archetypes come in four flavors, basic,minimal, sandbox and sandbox demo with each one being based on the previous one. In part 1, we&#8217;ll give an overview of the archetypes and the structure and configuration used in all of the archetypes.<!--more--><\/p>\n<h1>The Java EE 6 Archetypes<\/h1>\n<p>These Maven archetypes will help developers to get up and running with a new Java EE project quickly by creating the project structure that includes pre-defined configuration files. This lets developers create a project, remove parts they don&#8217;t need, and tweak those they want to keep. The balance is to provide something useful without burdening developers with a bunch of unwanted code and configuration. The basic and minimal archetypes serve this purpose. The basic archetype contains virtually no content, just configuration and a JSF page to confirm that it is working. The minimal archetype contains just a single page of content that demonstrates that JSF, JPA, CDI and validation are working. There are also two additional archetypes for servlet containers that can be used to run core features of Java EE 6 in a servlet contain such as Tomcat or Jetty.<\/p>\n<p>The sandbox archetype takes things a bit further by adding a simple JPA model pre-populated with data. This gives developers a sandbox to play in with everything configured and data to use so they can test out ideas or play around with different pieces of Java EE. I also plan on using it as the foundation project for tutorials and articles on Java EE 6 that developers can follow along with. It removes the need to create and document creating a project with a data model and example data for each tutorial. With a pre-built tutorial project we can dive right in to the meat of the tutorial without the boilerplate.<\/p>\n<p>The sandbox-demo archetype takes the sandbox application and extends it into a full featured application (albeit a contrived one) that demonstrates a number of features of Java EE 6, and gives developers an idea on how to get started developing applications using the Java EE stack. The demo is by no means perfect, it is mostly the bare minimum required to implement the necessary functionality. Again, it provides a good project to launch tutorials from by showing developers how to fix those little problems in the demo application. The demo also fills a gap in demonstrating to new developers how to work with these different technologies and integrate them.<\/p>\n<p>These articles will cover the different archetypes and explain the functionality and features in each. We&#8217;ll start by covering the very basics, which is the creation of the project structure and configuration in the basic archetype which is also shared with all of the archetypes.<\/p>\n<h1>The jee6-basic-archetype<\/h1>\n<p>The basic archetype is a Maven project that is configured and ready to deploy in a Java EE 6 container. It contains the configuration files necessary to start up the Java EE services such as CDI, JPA and JSF as well as the dependencies in the <code>pom.xml<\/code> file.<\/p>\n<p\/>\nIn the root directory of the new project, we have the <code>pom.xml<\/code> file and a <code>readme.txt<\/code> which contains a description of the project and notes on how to deploy it.<br \/>\nThe <code>src\/main\/resources\/<\/code> folder contains resources that the application may need. The <code>META-INF<\/code> sub folder contains the <code>persistence.xml<\/code> file which defines the JPA datasource name and sets properties for the JPA provider. If you are deploying to JBoss you are all set to go with the <code>DefaultDS<\/code> datasource, but for Glassfish, you will need to specify the <code>jdbc\/__default<\/code> datasource instead which is commented out in the file, and remove the <code>DefaultDS<\/code> definition.<\/p>\n<p\/>\nThe main web content resides in the <code>src\\main\\webapp<\/code> folder which is structured as so :<\/p>\n<div class=\"figure\" style=\"width : 90%\">\n<h1>Web folder structure and content<\/h1>\n<pre>\r\nsrc\/main\/webapp\t\t\t  Top level webapp folder\r\n |\t\t\t\r\n |-META-INF\t\t\t\r\n |    |-context.xml\t \t  Configuration file for tomcat\r\n |\t\t\t\r\n |-resources\t\t\t  Resources folder for JSF resources\r\n |  |-css\t\t\t  CSS folder for stylesheets\r\n |     |\t\t\t\r\n |     |-screen.css\t \t  Default stylesheet\r\n |\t\t\t\r\n |-WEB-INF\t\t\t  Standard WEB-INF folder\r\n |   |-templates\t   \t  Templates folder for JSF templates\r\n |   |    |-template.xhtml     \t  Default template file\r\n |   |\t\t\t\r\n |   |-beans.xml\t\t  Indicates that this archive file has CDI beans\r\n |   |-faces-config.xml\t\t  Configuration file for JSF\r\n |   |-web.xml\t\t\t  web.xml file containing JSF configuration\r\n |\t\t\t\r\n |-home.xhtml\t\t\t  Default home page using the WEB-INF\\template\\template.xhtml\r\n |-index.html\t\t\t  Default file used to redirect to the home.jsf page\r\n<\/pre>\n<\/div>\n<p>The <code>beans.xml<\/code>,<code>faces-config.xml<\/code> and the <code>web.xml<\/code> files have very little in them. The <code>web.xml<\/code> file provides the definition and mapping for the the faces servlet. The <code>beans.xml<\/code> is required to signal the container that this archive contains CDI beans and should be processed by the CDI deployer. This file could have other content if you use alternatives, decorators or interceptors.<\/p>\n<div class=\"sidenote_right\">\n<h1>Servlet Schema Version<\/h1>\n<p>The schema version for the <code>web.xml <\/code> defaults to 2.5 because of problems with Eclipse not recognizing the schema as valid when creating the project. For the most part, everything will work the same, but if it must be version 3.0 or later you can change once the project has been created without any problems.\n<\/div>\n<p>Pretty much everything in the <code>webapp<\/code> folder can be used in a new application, and it isn&#8217;t that difficult to rename and move elements (templates, page file names etc) to get everything how you want it set up. In general, xml files shouldn&#8217;t be moved or renamed, but everything else can be.  <\/p>\n<p\/>\nIf you are starting a new project, your first steps would be to move\/rename\/edit the template, <code>home.xhtml<\/code> and <code>screen.css<\/code> pages to your liking. You would also want to delete the <code>readme.txt<\/code> file.<\/p>\n<p\/>\nThe bulk of the content is in the  <code>pom.xml<\/code> file which specifies all the depencies the project uses, most of which have the scope of <code>provided<\/code> since we anticipate the server providing the dependencies. <\/p>\n<p\/>\nAll of the archetypes also contain some code for producing an <code>EntityManager<\/code> in the application using a Stateless EJB implementing a Local interface, and a CDI qualifier to specifying injection points for the entity manager produced. <\/p>\n<ul>\n<li><b><code>DataRepository.java<\/code><\/b> &#8211; Qualifier for the <code>EntityManager<\/code> instance.<\/li>\n<li><b><code>DataRepositoryProducer.java<\/code><\/b> &#8211; Stateless EJB that produces the <code>EntityManager<\/code><\/li>\n<li><b><code>DataRepositoryProducerLocal.java<\/code><\/b> &#8211; Local interface for the stateless EJB<\/li>\n<\/ul>\n<p>Technically, the Local interface shouldn&#8217;t be needed under EJB 3.1 which is part of Java EE 6, however Weld, the reference implementation of CDI has problems with EJBs without a local interface in that it cannot locate them. To see how to use the produced <code>EntityManager<\/code>, see the next part which covers the minimal archetype that has a small JPA data model.<\/p>\n<p>This about wraps it up for configuration, there is very little added to the core project that you probably won&#8217;t need for a JSF\/CDI\/JPA application making the basic archetype a good choice for a new project where you don&#8217;t need the smoke tests to see if everything is setup and working.<\/p>\n<p>Next time we&#8217;ll look at the minimal archetype in detail which contains somewhat more code and functionality to test that JSF\/CDI\/JPA is working as planned.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This set of articles will document the contents of the Java EE archetypes for Maven. The archetypes come in four flavors, basic,minimal, sandbox and sandbox demo with each one being based on the previous one. In part 1, we&#8217;ll give an overview of the archetypes and the structure and configuration used in all of the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[63],"tags":[66,49,20,72,27,32,61],"_links":{"self":[{"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/posts\/1093"}],"collection":[{"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/comments?post=1093"}],"version-history":[{"count":6,"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/posts\/1093\/revisions"}],"predecessor-version":[{"id":1360,"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/posts\/1093\/revisions\/1360"}],"wp:attachment":[{"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/media?parent=1093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/categories?post=1093"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.andygibson.net\/blog\/wp-json\/wp\/v2\/tags?post=1093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}