笔记

way

JBoss Forge 2入门教程

    《Continuous Enterprise Development in Java》引入了对Forge的使用,但是都是针对Forge 1版本编写的。现在最新版的JBoss Developer Studio 9 beta自带的都是2.15版本。使用JBoss Developer Studio内嵌的Forge遇到很多问题,干脆直接下载了Forge 2.19使用命令行进行试用。现在对Forge 2的介绍实在太少,结合这本书和官方文档整理了一下入门教程。
    一、介绍
    如果你花费了时间开发Jave EE基础的项目 (或者任何大型点的项目), 你很可能花大量精力创建项目布局,定义依赖,告知在编译和执行过程中用到的相关类路径。虽然使用Maven相比手动创建项目会减少很多负担,但一般来说在pom.xml中还是有很多模板来定义需求。
    JBoss Forge 为Java EE项目提供增量项目加强。 以命令shell和集成到一些IDE中的实现, Forge使我们能改变项目文件和文件夹。一些可以使用Forge处理的具体任务有:
• 增加JPA实体,描述他们的model
• 配置Maven依赖
• 项目支架搭建
• 从域模型里面逆向工程产生视图层布局
• 部署到应用服务器
从头开始建项目的话使用Forge来创建项目布局比较有意义。最终会给我们一个从数据库到视图层的功能性骨架,我们能用来作为学习工具或写真实代码的快捷方式。Forge的用户界面是一个shell,因此可以手工安装并从终端使用(像其他命令行应用一样)。写命令行过程中,支持Tab键提示
【Forge和spring roo类似,不过没有依赖于spring相关,完全符合JEE的标准】
   二、下载安装Forge
在官网下载压缩包,解压配置环境变量。具体过程略。
   三、
创建一个Maven为基础的Java EE项目
命令行运行forge,切换到工作空间目录下。创建
叫feedback的简单应用,它允许用户添加评论。
 [jbdevstudio-workspace]$ project-new --named feedback --topLevelPackage org.cedj.feedback --targetLocation  feedback;
--targetLocation /directory/path //在该文件夹下创建项目
--topLevelPackage //groupId
--finalName //build中的finalName
--type //默认是war, 可选"jar" "parent" "addon" "ear" "resource-jar" "from-archetype" 
【使用man 命令可看到一些命令的说明】
现在可看到生成了一个标准的maven项目。

    四、
建立JPA和创建实体
[feedback]$ jpa-new-entity --named FeedbackEntry
这个命令有几个作用,首先它在正确的地方创建persistence.xml(在META-INF文件夹下),默认是Hibernate配置。 然后在子包model下创建实体。注意这个实体已经有一个id和version以及他们的getters/setters. Forge的好处就是一个命令就全部创建好可以用了。
    如果你不愿意用Java EE容器的默认数据源,你可以指定额外的连接选项比如JNDI data-source names, JDBC connection information, and data-source types. 注意, 这样的话你可能配置你的应用服务器来提供这个新的data-source and/or database connection.
[feedback]$ jpa-setup --provider Eclipse Link --dbType POSTGRES --dataSourceName java:comp/DefaultDataSource
如果执行了jpa-setup命令,你会发现提示符变化了:从[feedback] 变到了[persistence.xml]. 在Forge中,提示符总是告诉你在什么地方。 你能输入命令pwd或者ls。 现在我们回到feedback实体: 输入cd ~~ 回到根目录,再输入cd src/main/java/org/cedj/model/FeedbackEntry.java.
    五、
在实体上创建新的域
FeedbackEntry实体应该可以用Twitter ID记录一些用户的反馈。我们加字段(以及验证他们不为null):
[FeedbackEntry.java]$ jpa-new-field --named twitterHandle --type String
[FeedbackEntry.java]$ jpa-new-field --named feedback --type String
 Forge有许多选项快速创建属性和定制它们的JPA映射。比如我们想指定反馈由什么语言写成。我们能使用Forge快速创建一个Java枚举类型,再把它用到实体中 :
FeedbackEntry$ java-new-enum --named Language --targetPackage org.cedj.feedback.model
[Language.java]$ java-new-enum-const ENGLISH
[Language.java]$ java-new-enum-const FRENCH
注意提示符,Forge有很多命令行在目录、类或文件之间浏览。回到FeedbackEntry实体的命令:
[Language.java]$ cd ..
[model]$ cd FeedbackEntry.java
[FeedbackEntry]$ 
创建枚举域:
[FeedbackEntry]$ jpa-new-field --named language --type org.cedj.feedback.model.Language
默认JPA域是字符串类型。使用--type选项,我们可选基本数据类型(int, byte, char), enum,或其他实体和实体关联 (One-to-One, One-to-Many, Many-to-One, Many-to-Many). 我们再创建一个新的Author实体,增加一个Many-to-One 关系到feedback:
$ jpa-new-entity --named Author
[Author.java]$ jpa-new-field --named firstName
[Author.java]$ cd ../FeedbackEntry.java
[FeedbackEntry]$ jpa-new-field --named author --type org.cedj.feedback.model.Author --relationshipType Many-to-One
    六、
在实体上增加Bean验证限制
Java EE6引入了Bean Validation规范,这个规范允许一个声明来验证限制全部数据库、应用和视图层。
[FeedbackEntry.java]$ constraint-add --onProperty twitterHandle --constraint NotNull
[FeedbackEntry.java]$ constraint-add --onProperty feedback  --constraint NotNull
Forge在后台创建validation.xml文件,增加Bean验证依赖和需要的限制。
    七、
现在创建UI层来看我们的web应用
JSF是默认的Java EE用户界面框架,因此JBoss Forge对它有许多支持。事实上只需一个命令, Forge能轻易从JPA实体搭建整个CRUD web应用。JSF生成的应用遵循几个模式和最佳实践: usage of CDI conversation scope, the extended persistence context, JSF converters and so on.
我们可以根据某一个实体来搭建,或使用通配符让Forge为每一个实体搭建。
[FeedbackEntry.java]$scaffold-generate --targets org.cedj.feedback.model.*;
    一个命令,Forge就为FeedbackEntry和Author生成了配置文件(web.xml, faces-config.xml, …), JSF页面,为布局生成了图片,CSS以及加入了Bootstrap.
    默认Forge使用JSF 2.0搭建一个web应用,当你可执行faces-setup命令来改变这个配置。事实上,大多数Forge命令可以被setup (e.g. jpa-setup, servlet-setup…)
 $ faces-setup --facesVersion 2.2
    八、 创建Arquillian 测试
Arquillian是一个针对JVM的创新的、高扩展测试平台,它能使开发人员轻易为Java中间件创建自动化集成、功能性和验收测试。 接手单元测试留下的工作,Arquillian处理所有容器管理、部署和框架初始化,这样你就能集中在手中的任务,写测试。 真正的测试(应该指的是不用Mock的方式)。简而言之,Arquillian brings the test to the runtime so you don‘t have to manage the runtime from the test (or the build). Arquillian通过覆盖测试执行的所有方面来去掉了这个负担,测试执行过程包括:
  • 管理容器的生命周期

  • Bundling the test case, dependent classes and resources into a ShrinkWrap archive (or archives)

  • Deploying the archive (or archives) to the container (or containers)

  • Enriching the test case by providing dependency injection and other declarative services

  • Executing the tests inside (or against) the container

  • Capturing the results and returning them to the test runner for reporting

    为了避免引入不必要的复杂性到开发人员的构建环境, Arquillian 无缝集成了熟悉的测试框架(比如JUnit 4, TestNG 5), 运行使用存在的IDE、Ant和Maven测试插件来启动测试(不用任何插件)。

先看看怎么安装
Arquillian插件
就像安装wildfly插件一样,在Forge中使用Arquillian能力只需要安装一个插件。最简单的安装方法是在Forge console中执行命令直接从Git库中安装
$addon-install-from-git --url https://github.com/forge/addon-arquillian.git --coordinate org.arquillian.forge:arquillian-addon 
 连接到插件库,抓取请求插件的最新版本,构建源代码,把二进制文件安装到当前运行时。因为Forge是在一个模块化类加载架构上构建的,我们能在不重启进程,也不需要考虑冲突依赖的情况下加载插件。
    Arquillian安装好之后,我们可以访问arquillian命令了。安装成功后输入arq按TAB键就能看到arquillian-setup命令。Arquillian支持多个容器中的测试。这是通过为每一个容器使用一个Maven profile来实现的。这样我们也能同时配置多个容器了。
    先让Forge添加POM的依赖,这样我们就能在wildfly容器上运行Arquillianr测试了:
$arquillian-setup  --containerType REMOTE --containerName JBOSS_AS_REMOTE_7.X --testframework //书上的命令,下面的是修改过的命令
$ arquillian-setup --arquillianVersion 1.1.5.Final --testFramework junit --testFrameworkVersion 4.11 --containerAdapter wildfly-remote --containerAdapterVersion 8.1.0.Final,这个命令会在pom.xml中添加运行Arquillian测试所有需要的依赖 (JUnit, Arquillian core, Arquillian extension for WildFly) 还有Maven Profile (上面的命令生成的是arquillian-wildfly-remote) 。This command has also created an arquillian.xml file where all the Arquillian configuration goes. As you can see, with a single command, JBoss Forge has dealt with all the plumbing configuration.
需要选择相应配置
POM配置会做相应修改,创建写测试的出发点:
$ arquillian-create-test --targets org.cedj.ch03.feedback.model.FeedbackEntry
这个类没有引用任何应用服务器或目标容器。因为Arquillian被设计来从目标运行时中解耦出测试的编程模型;任何能处理测试要求能力的容器都能工作。这能使Java EE的可移植性目标不受到影响,把启动和部署的结构移到配置元素中。 In this case, the Arquillian runner会发现wildfly容器adaptor在编译路径上(因为运行
Arquillian Forge插件的setup命令的时候,adapter被定义在POM)
生成的FeedbackEntry类中可看到    @RunWith(Arquillian.class)
    @RunWith是一个标准的JUnit构件用来把控制指向特定的test runner.这是Arquillian的入口;从这里Arquillian可以从JUnit收获生命周期事件,然后自己处理。这样设计的好处是,就用户而言Arquillian不需要特殊的插件或配置。 任何能启动JUnit测试的—可以是Maven build, an Ant task, a manual command,or an IDE—都能利用 Arquillian 而无需额外的处理。For instance, you can use JBDS and Eclipse to launch a full-scale integration test with Arquillian by right-clicking on the class and selecting Run As → JUnit Test.
    public class FeedbackEntryTest {...}
The important bit here is what’s not required. Because of the Arquillian JUnit Test Runner, you’re free to use whatever class hierarchy you’d like, and there’s no need to extend a base support class. This keeps Arquillian tests in line with the POJO programming model originally introduced in Java EE5.
要让测试类运行,首先确保WildFly安装、运行。
安装JBoss AS Forge addon
安装Wildfly插件:
$ addon-install-from-git --url https://github.com/jerr/as-addon  --coordinate org.jboss.forge.addon:as,2.0.0-SNAPSHOT
$addon-install-from-git --url https://github.com/jerr/jboss-as-addon  --coordinate org.jboss.forge.addon:jboss-as-wf8,2.0.0-SNAPSHOT
$ as-setup --server wildfly8
需要下载一段时间,安装成功后,可以看到as-的一些命令
$ as-
as-deploy  as-setup  as-shutdown  as-start  as-undeploy
接着构建应用,使用as-start启动JBOSS,使用as-deploy部署应用:
$ build
$ as-start
(...)
JBoss logs
(...)
$ as-deploy
现在可在localhost:8080/feedback看见应用了。
也可以跑arquillian测试了:mvn install -Parquillian-wildfly-remote【这里我直接另外开了一个console,运行maven命令,-P后面的参数是之前在pom.xml中生成的profile】
注:在win 7 64位上使用Forge 2.19命令行时发现键盘上下键出现乱码,还不清楚是不是环境原因。

posted on 2015-09-23 21:14 yuxh 阅读(4369) 评论(0)  编辑  收藏 所属分类: j2ee


只有注册用户登录后才能发表评论。


网站导航:
 

导航

<2015年9月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

统计

常用链接

留言簿

随笔分类

随笔档案

收藏夹

博客

搜索

最新评论

阅读排行榜

评论排行榜