Maven+glassfish基礎搭建與使用

Build Tools

即構建工具是一個把源代碼生成可執行應用程序的過程自動化的程序(例如Android app生成apk)。由Apache軟件基金會所提供。基於項目對象模型(縮寫:POM)概念,Maven利用一箇中央信息片段能管理一個項目的構建、報告和文檔等步驟,構建包括編譯、鏈接跟把代碼打包成可用的或可執行的形式。html

1.構建工具的做用

  依賴管理、測試、打包、發佈java

2.主流的構建工具

  • Ant:提供編譯測試、打包
  • Maven:在Ant的基礎上提供依賴管理和發佈的功能
  • Gradle:在Maven的基礎上使用Groovy管理腳本,再也不使用XML來管理

3.爲何要使用構建工具?

       在小型項目,開發人員經常會手動調用構建 的過程。 這不是實際對於較大的項目,很是 很難跟蹤須要構建什麼,序列和 依賴關係構建過程當中有什麼。 使用一個 自動化工具容許構建過程更一致。git

Glassfish

GlassFish 是一款強健的商業兼容應用服務器,達到產品級質量,可免費用於開發、部署和從新分發。開發者能夠免費得到源代碼,還能夠對代碼進行更改。github

Glassfish與Tomcat的區別

Tomcat只是WEB容器,並不支持EJB,而Glassfish既是WEB容器也是EJB容器。web

Playing maven+glassfish

一、配置Maven

  ①下載官方maven3.6(這裏是下載的Windows版本)apache

  

  ②解壓apache-maven-3.6.0-bin.zip文件windows

  ③配置環境變量api

    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的配置與安裝》

二、搭建glassfish

  ①下載官方glassfish5(這裏下載完整平臺)

  

 

  ②解壓glassfish-5.0文件

  ③配置環境變量

    1°右擊計算機—屬性—高級系統設置—環境變量

    2°在系統變量Path增長maven的相關信息,路徑到bin爲止

    

  ④安裝完成

三、運行實例

  ①下載並解壓實例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網站,在輸入框中輸入你的名字,進行簡單的互動。

  

相關文章
相關標籤/搜索