Maven+glassfish基础搭建与使用
Build Tools
即构建工具是一个把源代码生成可执行应用程序的过程自动化的程序(例如Android app生成apk)。由Apache软件基金会所提供。基于项目对象模型(缩写:POM)概念,Maven利用一个中央信息片断能管理一个项目的构建、报告和文档等步骤,构建包括编译、连接跟把代码打包成可用的或可执行的形式。
1.构建工具的作用
依赖管理、测试、打包、发布
2.主流的构建工具
- Ant:提供编译测试、打包
- Maven:在Ant的基础上提供依赖管理和发布的功能
- Gradle:在Maven的基础上使用Groovy管理脚本,不再使用XML来管理
3.为什么要使用构建工具?
在小型项目,开发人员常常会手动调用构建 的过程。 这不是实际对于较大的项目,非常 很难跟踪需要构建什么,序列和 依赖关系构建过程中有什么。 使用一个 自动化工具允许构建过程更一致。
Glassfish
GlassFish 是一款强健的商业兼容应用服务器,达到产品级质量,可免费用于开发、部署和重新分发。开发者可以免费获得源代码,还可以对代码进行更改。
Glassfish与Tomcat的区别
Tomcat只是WEB容器,并不支持EJB,而Glassfish既是WEB容器也是EJB容器。
Playing maven+glassfish
1、配置Maven
①下载官方maven3.6(这里是下载的Windows版本)
②解压apache-maven-3.6.0-bin.zip文件
③配置环境变量
1°右击计算机—属性—高级系统设置—环境变量
2°在系统变量Path增加maven的相关信息,路径到bin为止
④win+R,输入cmd进入命令行模式
⑤输入:mvn -v,出现maven的信息则说明maven安装成功
【注】若出现错误:An Internal error occured during "creating project firstMavenAPP",java.lang.NullPointer
Exception......其实就是JDK版本和maven版本不兼容所导致的,这个官网也有说明。
重新安装正确的JDK即可解决,关于JDK的安装可以参考上一篇博客《Tomcat的配置与安装》
2、搭建glassfish
①下载官方glassfish5(这里下载完整平台)
②解压glassfish-5.0文件
③配置环境变量
1°右击计算机—属性—高级系统设置—环境变量
2°在系统变量Path增加maven的相关信息,路径到bin为止
④安装完成
3、运行实例
①下载并解压实例tutorial-examples (这里可以通过官网下载,也可以采用git工具:git clone https://github.com/javaee/tutorial-examples)
②win+R输入cmd进入命令行,进入刚刚下载的tutorial-examples中,并转到E:\IntelliJ IDEA\tutorial-examples-master\web\jsf\hello1(这里是我的文件目录)
③maven打包
在命令行中输入mvn clean package
这时会发现hello1中多了一个target文件夹,则说明打包成功
【注:若出现打包mvn install错误】
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 15:47 min
[INFO] Finished at: 2019-03-10T21:26:26+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.cargo:cargo-maven2-plugin:1.4.4:redeploy (deploy) on project hello1: Execution deploy of goal org.codehaus.cargo:cargo-maven2-plugin:1.4.4:redeploy failed: Failed to create deployer with implementation class org.codehaus.cargo.container.glassfish.GlassFish4xInstalledLocalDeployer for the parameters (container [id = [glassfish4x]], deployer type [installed]).: InvocationTargetException: The container configuration directory "c://glassfish5/glassfish/domains" does not exist. Please configure the container before attempting to perform any local deployment. Read more on: http://cargo.codehaus.org/Local+Configuration -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
将目录tutorial-examples-master/pox.xml文件打开
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 2 3 <modelVersion>4.0.0</modelVersion> 4 <parent> 5 <groupId>net.java</groupId> 6 <artifactId>jvnet-parent</artifactId> 7 <version>5</version> 8 </parent> 9 <groupId>org.glassfish.javaeetutorial</groupId> 10 <artifactId>javaeetutorial</artifactId> 11 <version>8.1-SNAPSHOT</version> 12 <packaging>pom</packaging> 13 <name>Java EE Tutorial Examples</name> 14 15 <scm> 16 <connection>scm:git:https://github.com/javaee/tutorial-examples.git</connection> 17 <developerConnection>scm:git:git@github.com:javaee/tutorial-examples.git</developerConnection> 18 <tag>HEAD</tag> 19 </scm> 20 <issueManagement> 21 <system>GitHub</system> 22 <url>https://github.com/javaee/tutorial-examples/issues</url> 23 </issueManagement> 24 25 <properties> 26 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 27 <javaee.api.version>8.0</javaee.api.version> 28 <maven.compiler.plugin.version>3.1</maven.compiler.plugin.version> 29 <maven.source.plugin.version>2.2.1</maven.source.plugin.version> 30 <maven.clean.plugin.version>2.5</maven.clean.plugin.version> 31 <maven.war.plugin.version>2.3</maven.war.plugin.version> 32 <maven.acr.plugin.version>1.0</maven.acr.plugin.version> 33 <maven.ear.plugin.version>2.8</maven.ear.plugin.version> 34 <maven.ejb.plugin.version>2.3</maven.ejb.plugin.version> 35 <maven.jar.plugin.version>2.4</maven.jar.plugin.version> 36 <maven.rar.plugin.version>2.3</maven.rar.plugin.version> 37 <maven.license.plugin.version>1.10.b1</maven.license.plugin.version> 38 <maven.release.plugin.version>2.5.2</maven.release.plugin.version> 39 <maven.exec.plugin.version>1.2.1</maven.exec.plugin.version> 40 <junit.version>4.11</junit.version> 41 <eclipselink.version>2.5.0</eclipselink.version> 42 <glassfish.embedded.version>4.0</glassfish.embedded.version> 43 <cargo.plugin.version>1.4.4</cargo.plugin.version> 44 <glassfish.domain.name>domain1</glassfish.domain.name> 45 <glassfish.home>${glassfish.home.prefix}/glassfish5</glassfish.home> 46 <integration.container.id>glassfish4x</integration.container.id> 47 <maven.deploy.skip>true</maven.deploy.skip> 48 <maven.javadoc.skip>true</maven.javadoc.skip> 49 <maven.source.skip>true</maven.source.skip> 50 <maven.source.attach>false</maven.source.attach> 51 </properties> 52 53 <profiles> 54 <profile> 55 <id>windows</id> 56 <activation> 57 <os> 58 <family>windows</family> 59 </os> 60 </activation> 61 <properties> 62 <glassfish.home.prefix>E:/IntelliJ IDEA/glassfish-5.0/</glassfish.home.prefix> 63 <glassfish.executables.suffix>.bat</glassfish.executables.suffix> 64 </properties> 65 </profile> 66 <profile> 67 <id>unix</id> 68 <activation> 69 <os> 70 <family>unix</family> 71 </os> 72 </activation> 73 <properties> 74 <glassfish.home.prefix>${user.home}</glassfish.home.prefix> 75 <glassfish.executables.suffix /> 76 </properties> 77 </profile> 78 <profile> 79 <id>sdk</id> 80 <activation> 81 <activeByDefault>true</activeByDefault> 82 </activation> 83 <properties> 84 <glassfish.home>${basedir}/../../../</glassfish.home> 85 </properties> 86 </profile> 87 <profile> 88 <id>development</id> 89 <activation> 90 <file> 91 <exists>${basedir}/../bundle</exists> 92 </file> 93 </activation> 94 <properties> 95 <glassfish.home>${glassfish.home.prefix}/glassfish5</glassfish.home> 96 </properties> 97 </profile> 98 <profile> 99 <id>standalone</id> 100 <properties> 101 <glassfish.home>${basedir}/target/cargo/installs/glassfish</glassfish.home> 102 <cargo.maven.containerUrl>http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/latest-glassfish.zip</cargo.maven.containerUrl> 103 </properties> 104 </profile> 105 </profiles> 106 107 <modules> 108 <module>archetypes</module> 109 <module>batch</module> 110 <module>case-studies</module> 111 <module>cdi</module> 112 <module>concurrency</module> 113 <module>connectors</module> 114 <module>ejb</module> 115 <module>jaxrs</module> 116 <module>jaxws</module> 117 <module>jms</module> 118 <module>persistence</module> 119 <module>security</module> 120 <module>web</module> 121 </modules> 122 123 <repositories> 124 <repository> 125 <id>snapshot-repository.java.net</id> 126 <name>Java.net Snapshot Repository for Maven</name> 127 <url>https://maven.java.net/content/repositories/staging/</url> 128 <layout>default</layout> 129 </repository> 130 <repository> 131 <id>releases-repository.java.net</id> 132 <name>Java.net releases Repository for Maven</name> 133 <url>https://maven.java.net/content/repositories/releases/</url> 134 <layout>default</layout> 135 </repository> 136 </repositories> 137 138 <build> 139 <pluginManagement> 140 <plugins> 141 <plugin> 142 <groupId>org.apache.maven.plugins</groupId> 143 <artifactId>maven-compiler-plugin</artifactId> 144 <version>${maven.compiler.plugin.version}</version> 145 </plugin> 146 <plugin> 147 <groupId>org.apache.maven.plugins</groupId> 148 <artifactId>maven-source-plugin</artifactId> 149 <version>${maven.source.plugin.version}</version> 150 </plugin> 151 <plugin> 152 <groupId>org.apache.maven.plugins</groupId> 153 <artifactId>maven-clean-plugin</artifactId> 154 <version>${maven.clean.plugin.version}</version> 155 </plugin> 156 <plugin> 157 <groupId>org.apache.maven.plugins</groupId> 158 <artifactId>maven-war-plugin</artifactId> 159 <version>${maven.war.plugin.version}</version> 160 </plugin> 161 <plugin> 162 <groupId>org.codehaus.cargo</groupId> 163 <artifactId>cargo-maven2-plugin</artifactId> 164 <version>${cargo.plugin.version}</version> 165 </plugin> 166 </plugins> 167 </pluginManagement> 168 <plugins> 169 <plugin> 170 <groupId>org.codehaus.cargo</groupId> 171 <artifactId>cargo-maven2-plugin</artifactId> 172 <inherited>true</inherited> 173 <executions> 174 <execution> 175 <id>deploy</id> 176 <phase>integration-test</phase> 177 <goals> 178 <goal>redeploy</goal> 179 </goals> 180 </execution> 181 </executions> 182 <configuration> 183 <container> 184 <containerId>${integration.container.id}</containerId> 185 <type>installed</type> 186 <home>${glassfish.home}</home> 187 </container> 188 <configuration> 189 <type>existing</type> 190 <home>${glassfish.home}/glassfish/domains</home> 191 <properties> 192 <cargo.glassfish.domain.name>${glassfish.domain.name}</cargo.glassfish.domain.name> 193 <!--cargo.remote.username></cargo.remote.username--> 194 <cargo.remote.password /> 195 </properties> 196 </configuration> 197 </configuration> 198 </plugin> 199 <plugin> 200 <groupId>org.apache.maven.plugins</groupId> 201 <artifactId>maven-compiler-plugin</artifactId> 202 <version>${maven.compiler.plugin.version}</version> 203 <configuration> 204 <source>1.8</source> 205 <target>1.8</target> 206 </configuration> 207 </plugin> 208 <plugin> 209 <groupId>com.mycila.maven-license-plugin</groupId> 210 <artifactId>maven-license-plugin</artifactId> 211 <version>${maven.license.plugin.version}</version> 212 <configuration> 213 <header>common/license.txt</header> 214 <excludes> 215 <exclude>common/**</exclude> 216 <exclude>**/META-INF/**</exclude> 217 <exclude>**/WEB-INF/**</exclude> 218 <exclude>**/nbactions.xml</exclude> 219 <exclude>**/nb-configuration.xml</exclude> 220 <exclude>**/glassfish-resources.xml</exclude> 221 <exclude>**/simple-flow-flow.xml</exclude> 222 </excludes> 223 </configuration> 224 </plugin> 225 <plugin> 226 <artifactId>maven-release-plugin</artifactId> 227 <version>${maven.release.plugin.version}</version> 228 <configuration> 229 <mavenExecutorId>forked-path</mavenExecutorId> 230 <useReleaseProfile>false</useReleaseProfile> 231 <arguments>${release.arguments}</arguments> 232 <autoVersionSubmodules>true</autoVersionSubmodules> 233 <tagNameFormat>@{project.version}</tagNameFormat> 234 </configuration> 235 <dependencies> 236 <dependency> 237 <groupId>org.apache.maven.scm</groupId> 238 <artifactId>maven-scm-provider-gitexe</artifactId> 239 <version>1.9.4</version> 240 </dependency> 241 </dependencies> 242 </plugin> 243 </plugins> 244 </build> 245 246 247 <dependencies> 248 <dependency> 249 <groupId>javax</groupId> 250 <artifactId>javaee-api</artifactId> 251 <version>${javaee.api.version}</version> 252 <scope>provided</scope> 253 </dependency> 254 </dependencies> 255 256 </project>
将62行代码中<glassfish.home.prefix>E:/IntelliJ IDEA/glassfish-5.0/</glassfish.home.prefix>标签元素内容改为自己安装的glassfish的目录
④启动glassfish
创建新域:asadmain creat-domain XXX
启动服务:asadmain start-domain XXX (如果不选择相关域,则默认打开domain1)
关闭服务:asadmain stop-domain XXX
⑤打开浏览器输入localhost:4848即可进入glassfish控制页面(中间可能要输入账号和密码,看域是否有设置,domain1默认直接进入)
⑥部署web模块
这里我直接选择文件(E:\IntelliJ IDEA\tutorial-examples-master\web\jsf\hello1\target\hello.war),即刚刚maven打包的目录target下的hello1.war文件
完成web模块部署。
⑦查看web网站
即可看到加载hello1中的web网站,在输入框中输入你的名字,进行简单的互动。