AppFuse 2.0 快速起步
英文版网址:http://appfuse.org/display/APF/AppFuse+QuickStart
请依照以下步骤开始使用AppFuse开发Java EE应用程序:
目录
1. 建立一个新项目.
2. 运行.
建立项目
1. 下载安装JDK 5+.
2. 下载安装MySQL 5.x.
3. 设置一个本地的SMTP 服务器或者修改mail.properties文件(位于src/main/resources) 来使用其它的主机名-默认使用"localhost".
4. 下载 安装Maven 2.0.8.
5、下载appfuse2.0.1并解压appfuse
https://appfuse.dev.java.net/files/documents/1397/77082/appfuse-dependencies-2.0.1-with-source-and-javadoc.zip
假定解压到D:/appfuse2.0.1/repository下
6、修改%maven_home%/conf/settings.xml
修改settings.xml中的localRepository,指向appfuse的解压目录
<settings>
<!– localRepository
| The path to the local repository maven will use to store artifacts.
| Default: ~/.m2/repository
–>
<localRepository>D:/appfuse2.0.1/repository</localRepository>
修改原因:默认情况下 repository 在 C:"Documents and Settings"$USER_HOME ".m2"repository ,因为这个路径中包含一个空格,所以在后续使用 mvn appfuse:full-source 命令的时候会产生异常;
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'appfuse'.
[INFO] ------------------------------------------------------------------------
[INFO] Building AppFuse Modular Application - Core
[INFO] task-segment: [appfuse:full-source]
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[ERROR] FATAL ERROR
[INFO] ------------------------------------------------------------------------
[INFO] null
Illegal character in path at index 18: file:/C:/Documents and Settings/Administrator/.m2/repository/org/apache/ant/ant/1.7.0/ant-1.7.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] Trace
java.lang.IllegalArgumentException
at java.net.URI.create(URI.java:842)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tools.ant.launch.Locator.fromURI(Locator.java:162)
at org.apache.tools.ant.launch.Locator.getResourceSource(Locator.java:119)
at org.apache.tools.ant.launch.Locator.getClassSource(Locator.java:90)
at org.apache.tools.ant.Project.setAntLib(Project.java:313)
at org.apache.tools.ant.Project.initProperties(Project.java:309)
at org.apache.tools.ant.Project.init(Project.java:295)
官方解决地址http://www.nabble.com/Re%3A-problem-with-appfuse%3Agen-in-windows-p13974654s2369.html
意思是讲是ant的bug,里面不能有空格,因为Documents and Settings里面有空格所以报错。
另外,由于ant 1.7存在的bug(Problem with appfuse:gen in windows ),因此如果用的是ant 1.7,确认一下$USER_HOME/.m2/repository是否存在,不存在手工创建,并把修改后的settings.xml 拷贝到~".m2"repository下不然在执行后续操作mvn appfuse:full-source时候会报如下错误
java.lang.IllegalArgumentException: When not aligning with a user install you mu
st specify a local repository location using the setLocalRepositoryDirectory( Fi
le ) method.
at org.apache.maven.embedder.MavenEmbedder.createMavenSettings(MavenEmbe
dder.java:699)
at org.apache.maven.embedder.MavenEmbedder.start(MavenEmbedder.java:628)
at org.appfuse.mojo.installer.InstallSourceMojo.addModuleDependencies(In
stallSourceMojo.java:341)
at org.appfuse.mojo.installer.InstallSourceMojo.addModuleDependencies(In
stallSourceMojo.java:303)
at org.appfuse.mojo.installer.InstallSourceMojo.execute(InstallSourceMoj
o.java:142)
at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPlugi
nManager.java:443)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(Defa
ultLifecycleExecutor.java:539)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeStandalone
Goal(DefaultLifecycleExecutor.java:493)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(Defau
ltLifecycleExecutor.java:463)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHan
dleFailures(DefaultLifecycleExecutor.java:311)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegmen
ts(DefaultLifecycleExecutor.java:278)
at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLi
fecycleExecutor.java:143)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:334)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:125)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:280)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
[INFO] ————————————————————————
7.AppFuse拥有很多构造不同风格项目的能力. 为使用户使用起来更优化、更简单, 我们建立了多种原型(又称作:起始项目). 现在又两种类型的AppFuse 原型:基本的(basic)和模块化的(modular). 基本原型用于建立基于web应用的项目;模块化原型包含“核心”(core)"和"web"模块,用于后台可复用的项目。下表中的命令是用于产生其实项目的,改变groupId就定制包名(package name);改变artifactId来定制项目名(project's name). 进入D:"java"workspace,运行如下命令,生成struts2.0的项目
mvn archetype:create -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-basic-struts -DremoteRepositories=http://static.appfuse.org/releases -DarchetypeVersion=2.0.1 -DgroupId=cn.net.xinyi.nvs -DartifactId=nvsweb
在建立原型过程中,是会看到一些警告信息输出。如果在输出信息的末尾看到了“BUILD SUCCESSFUL”字样,那就意味着你的项目建立成功了。
|
Archetype
|
Command
|
JSF Basic
|
mvn archetype:create -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-basic-jsf -DremoteRepositories=http://static.appfuse.org/releases-DarchetypeVersion=2.0.1 -DgroupId=com.mycompany.app -DartifactId=myproject
|
Spring MVC Basic
|
mvn archetype:create -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-basic-spring -DremoteRepositories=http://static.appfuse.org/releases-DarchetypeVersion=2.0.1 -DgroupId=com.mycompany.app -DartifactId=myproject
|
Struts 2 Basic
|
mvn archetype:create -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-basic-struts -DremoteRepositories=http://static.appfuse.org/releases-DarchetypeVersion=2.0.1 -DgroupId=com.mycompany.app -DartifactId=myproject
|
Tapestry Basic
|
mvn archetype:create -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-basic-tapestry -DremoteRepositories=http://static.appfuse.org/releases-DarchetypeVersion=2.0.1 -DgroupId=com.mycompany.app -DartifactId=myproject
|
JSF Modular
|
mvn archetype:create -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-modular-jsf -DremoteRepositories=http://static.appfuse.org/releases-DarchetypeVersion=2.0.1 -DgroupId=com.mycompany.app -DartifactId=myproject
|
Spring MVC Modular
|
mvn archetype:create -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-modular-spring -DremoteRepositories=http://static.appfuse.org/releases-DarchetypeVersion=2.0.1 -DgroupId=com.mycompany.app -DartifactId=myproject
|
Struts 2 Modular
|
mvn archetype:create -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-modular-struts -DremoteRepositories=http://static.appfuse.org/releases-DarchetypeVersion=2.0.1 -DgroupId=com.mycompany.app -DartifactId=myproject
|
Tapestry Modular
|
mvn archetype:create -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-modular-tapestry -DremoteRepositories=http://static.appfuse.org/releases-DarchetypeVersion=2.0.1 -DgroupId=com.mycompany.app -DartifactId=myproject
|
Core
(backend only)
|
mvn archetype:create -DarchetypeGroupId=org.appfuse.archetypes -DarchetypeArtifactId=appfuse-core -DremoteRepositories=http://static.appfuse.org/releases-DarchetypeVersion=2.0.1 -DgroupId=com.mycompany.app -DartifactId=myproject
|
8. 如果你安装了MySQL 5.x而且它的root 用户没有设置口令(可以通过updateuser set password=password('') where user='root'; flush privileges;去掉root的密码),那你的AppFuse应用就能立即运行起来. 如果你想使用内嵌式的数据库(embedded database), 我们最近正好增加了一个Database Profiles 文件用于使用H2, HSQLDB等数据库.
关于MySQL 安全性
使用不设置口令的MySQL会导致系统不安全。一旦建立了数据库,可以使用以下命令来设置root用户的口令:
mysql --user=root --pass='' mysql -e "update user set password=password('newpw') where user='root'; flush privileges;"
AppFuse默认使用root用户和空口令,可以编辑项目的pom.xml文件(位于文件末尾)的和 属性,来设置相应的值
|
运行应用程序
现在,运行AppFuse是非常容易得. 一旦建立了原型项目,Maven 就会分别使用hibernate3and dbunit插件来为你建立并设置数据库.需要你做的只是使用Maven来运行Jetty容器并开始浏览应用了.
1. 除非你有更好的主意,应该首先把你的项目检入到源码控制服务器中。Google Code提供免费的Subversion 服务, 当然类似的还有很多.
2. 进入命令行, cd到你的新项目目录,运行mvn integration-test把JARs, Tomcat 和综合测试程序下载到项目里。现在是喝杯咖啡或啤酒和好时机,因为下载和运行测试大约需要5-10分钟.
3. 要浏览应用需在项目目录里运行命令 mvn jetty:run-war (对于模块化项目-modular project, 应在项目的web目录下运行mvn jetty:run-war命令). Maven 将会启动Jetty ,于是就可以在浏览器输入地址http://localhost:8080/来浏览应用了。
默认的管理用户的用户名和口令是dmin/admin,常规用户是user/user。
|
4. 运行命令 mvn war:inplace可以覆盖(override)AppFuse的文件.将会只把依赖的WARs文件抽取到src/main/webapp目录,在这里,你可以根据需要改变文件.当你把释放到你的源文件树时,可以运行 mvn jetty:run命令. 这样一来可以即时修改文件,而Jetty可以根据需要重新装载. 这种方式的唯一的问题是你的项目里的AppFuse将会是不完整("exploded AppFuse"), 会导致更新升级困难.我们推荐在运行命令mvn war:inplace以前,首先将项目检入源码控制系统。这样一来决定哪些文件被保留或删除文件会更容易些.
如果出现内存溢出(OutOfMemory )错误,请使用jetty:run命令,参照 this mailing list thread.
在项目目录里运行命令mvn appfuse:full-source可以将AppFuse的运行模式由内嵌("embedded mode")改变为全源码模式(full-source mode)。
改变数据库的设置
要改变MySQL 数据库的设置,可以简单地修改位于pom.xml末尾的 属性. 参照 Database Profiles 来使用非MySQL的其他数据库.
|
应用的部署
可以使用Eclipse, IDEA 或 NetBeans来部署应用.使用Eclipse,运行 mvn eclipse:eclipse来生成项目文件. 使用IDEA, 就要使用mvn idea:idea命令。进一步的介绍请参考IDE Reference Guide.