Maven构建web项目
Maven建立web项目
packaging方式为war。
在build>finalName
下指定最终生成的war包名,不然带版本号的war包部署不便。
<build> <finalName>${project.artifactId}</finalName> <build>
建立普通Java EE项目结构
mvn archetype:generate -DgroupId=hx.frame -DartifactId=frame-web \ -DarchetypeArtifactId=maven-archetype-webapp
加入全部JavaEE 7依赖:
<dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>7.0</version> <scope>compile</scope> </dependency>
压缩Js与Css
<!-- YUI Compressor Maven压缩插件 --> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>yuicompressor-maven-plugin</artifactId> <version>1.3.0</version> <configuration> <!-- 读取js,css文件采用UTF-8编码 --> <encoding>UTF-8</encoding> <!-- 不显示js可能的错误 --> <jswarn>false</jswarn> <!-- 无改动就不压缩 --> <force>false</force> <!-- 在指定的列号后换行 --> <linebreakpos>-1</linebreakpos> <!-- 压缩之前先执行聚合文件操作 --> <preProcessAggregates>true</preProcessAggregates> <!-- 压缩后保存文件后缀 --> <suffix>.min</suffix> <!-- 源目录,即需压缩的根目录 --> <sourceDirectory>${basedir}/mobile</sourceDirectory> <!-- 压缩js和css文件 --> <includes> <include>**/*.js</include> <include>**/*.css</include> </includes> <!-- 以下目录和文件不会被压缩 --> <excludes> <exclude>**/*.min.js</exclude> <exclude>**/*.min.css</exclude> <exclude>scripts/data/*.js</exclude> </excludes> <!-- 压缩后输出文件目录 --> <outputDirectory>${basedir}/mobile</outputDirectory> <!-- 聚合文件 --> <aggregations> <!-- 一个聚合项目 --> <aggregation> <!-- 合并每一个文件后插入一新行 --> <insertNewLine>true</insertNewLine> <!-- 需合并文件的根文件夹 --> <inputDir>${basedir}/mobile/scripts</inputDir> <!-- 最终合并的输出文件 --> <output>${basedir}/mobile/scripts/app/app.js</output> <!-- 把以下js文件合并成一个js文件,是按顺序合并的 --> <includes> <include>app/core.js</include> <include>app/mlmanager.js</include> <include>app/tmpl.js</include> <include>app/ui.js</include> </includes> </aggregation> <!-- css 文件压缩成一个文件 --> <aggregation> <!-- 合并每一个文件后插入一新行 --> <insertNewLine>true</insertNewLine> <!-- 需合并文件的根文件夹 --> <inputDir>${project.build.directory}</inputDir> <!-- 最终合并的输出文件 --> <output>${project.build.directory}/${project.build.finalName}/staicfile/css/selcss.pack.css</output> <!-- 把以下js文件合并成一个js文件,是按顺序合并的 --> <includes> <include>**/css/*.css</include> </includes> </aggregation> <!-- js 文件压缩成一个文件 --> <aggregation> <insertNewLine>true</insertNewLine> <inputDir>${project.build.directory}</inputDir> <output>${project.build.directory}/${project.build.finalName}/staicfile/js/seljs.pack.js</output> <includes> <include>**/js/*.js</include> </includes> <excludes> <exclude>**/*.min.js</exclude> <exclude>**/*-min.js</exclude> </excludes> </aggregation> </aggregations> </configuration> </plugin>
执行参数:
mvn yuicompressor:compress
发布web项目
Jetty插件
这个插件可以启动一个jetty web服务器,启动速度喜人,一旦启动,修改了jsp,页面上
可以直接反应出来,修改了servlet代码,运行一下mvn compile
也可以立马反应出来,
方便开发
在pom.xml中这样配置:
<build> ... <plugins> ... <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>9.2.0.RC0</version> <configuration> <httpConnector> <port>8080</port> </httpConnector> <!-- <jettyXml>jetty.xml,jetty-http.xml,jetty-ssl.xml,jetty-https.xml</jettyXml> --> <scanIntervalSeconds>10</scanIntervalSeconds> <webAppConfig> <contextPath>/jadeutils-cdn</contextPath> </webAppConfig> </configuration> </plugin> ... </plugins> ... </build>
然后命令行运行:mvn jetty:run
。访问一下:localhost:8080/test/
就可以看到
index.jsp了
还可以进行更加详细的配置:
jetty.xml
:
<New id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration"> <Set name="secureScheme">https</Set> <Set name="securePort"><Property name="jetty.secure.port" default="8443" /></Set> <Set name="outputBufferSize">32768</Set> <Set name="requestHeaderSize">8192</Set> <Set name="responseHeaderSize">8192</Set> <Set name="sendServerVersion">true</Set> <Set name="sendDateHeader">false</Set> <Set name="headerCacheSize">512</Set> <!-- Uncomment to enable handling of X-Forwarded- style headers <Call name="addCustomizer"> <Arg><New class="org.eclipse.jetty.server.ForwardedRequestCustomizer"/></Arg> </Call> --> </New>
jetty-http.xml
:
<?xml version="1.0"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"> <!-- ============================================================= --> <!-- Configure the Jetty Server instance with an ID "Server" --> <!-- by adding a HTTP connector. --> <!-- This configuration must be used in conjunction with jetty.xml --> <!-- ============================================================= --> <Configure id="Server" class="org.eclipse.jetty.server.Server"> <!-- =========================================================== --> <!-- Add a HTTP Connector. --> <!-- Configure an o.e.j.server.ServerConnector with a single --> <!-- HttpConnectionFactory instance using the common httpConfig --> <!-- instance defined in jetty.xml --> <!-- --> <!-- Consult the javadoc of o.e.j.server.ServerConnector and --> <!-- o.e.j.server.HttpConnectionFactory for all configuration --> <!-- that may be set here. --> <!-- =========================================================== --> <Call name="addConnector"> <Arg> <New class="org.eclipse.jetty.server.ServerConnector"> <Arg name="server"><Ref refid="Server" /></Arg> <Arg name="factories"> <Array type="org.eclipse.jetty.server.ConnectionFactory"> <Item> <New class="org.eclipse.jetty.server.HttpConnectionFactory"> <Arg name="config"><Ref refid="httpConfig" /></Arg> </New> </Item> </Array> </Arg> <Set name="host"><Property name="jetty.host" /></Set> <Set name="port"><Property name="jetty.port" default="80" /></Set> <Set name="idleTimeout"><Property name="http.timeout" default="30000"/></Set> <Set name="soLingerTime"><Property name="http.soLingerTime" default="-1"/></Set> </New> </Arg> </Call> </Configure>
jetty-https.xml
:
<?xml version="1.0"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"> <!-- ============================================================= --> <!-- Configure a HTTPS connector. --> <!-- This configuration must be used in conjunction with jetty.xml --> <!-- and jetty-ssl.xml. --> <!-- ============================================================= --> <Configure id="Server" class="org.eclipse.jetty.server.Server"> <!-- =========================================================== --> <!-- Add a HTTPS Connector. --> <!-- Configure an o.e.j.server.ServerConnector with connection --> <!-- factories for TLS (aka SSL) and HTTP to provide HTTPS. --> <!-- All accepted TLS connections are wired to a HTTP connection.--> <!-- --> <!-- Consult the javadoc of o.e.j.server.ServerConnector, --> <!-- o.e.j.server.SslConnectionFactory and --> <!-- o.e.j.server.HttpConnectionFactory for all configuration --> <!-- that may be set here. --> <!-- =========================================================== --> <Call id="httpsConnector" name="addConnector"> <Arg> <New class="org.eclipse.jetty.server.ServerConnector"> <Arg name="server"><Ref refid="Server" /></Arg> <Arg name="factories"> <Array type="org.eclipse.jetty.server.ConnectionFactory"> <Item> <New class="org.eclipse.jetty.server.SslConnectionFactory"> <Arg name="next">http/1.1</Arg> <Arg name="sslContextFactory"><Ref refid="sslContextFactory"/></Arg> </New> </Item> <Item> <New class="org.eclipse.jetty.server.HttpConnectionFactory"> <Arg name="config"><Ref refid="sslHttpConfig"/></Arg> </New> </Item> </Array> </Arg> <Set name="host"><Property name="jetty.host" /></Set> <Set name="port"><Property name="https.port" default="443" /></Set> <Set name="idleTimeout"><Property name="https.timeout" default="30000"/></Set> <Set name="soLingerTime"><Property name="https.soLingerTime" default="-1"/></Set> </New> </Arg> </Call> </Configure>
jetty-ssl.xml
:
<?xml version="1.0"?> <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd"> <!-- ============================================================= --> <!-- Configure a TLS (SSL) Context Factory --> <!-- This configuration must be used in conjunction with jetty.xml --> <!-- and either jetty-https.xml or jetty-spdy.xml (but not both) --> <!-- ============================================================= --> <Configure id="sslContextFactory" class="org.eclipse.jetty.util.ssl.SslContextFactory"> <Set name="KeyStorePath"><Property name="jetty.base" default="." />/<Property name="jetty.keystore" default="etc/keystore"/></Set> <Set name="KeyStorePassword"><Property name="jetty.keystore.password" default="OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4"/></Set> <Set name="KeyManagerPassword"><Property name="jetty.keymanager.password" default="OBF:1u2u1wml1z7s1z7a1wnl1u2g"/></Set> <Set name="TrustStorePath"><Property name="jetty.base" default="." />/<Property name="jetty.truststore" default="etc/keystore"/></Set> <Set name="TrustStorePassword"><Property name="jetty.truststore.password" default="OBF:1vny1zlo1x8e1vnw1vn61x8g1zlu1vn4"/></Set> <Set name="EndpointIdentificationAlgorithm"></Set> <Set name="NeedClientAuth"><Property name="jetty.ssl.needClientAuth" default="false"/></Set> <Set name="WantClientAuth"><Property name="jetty.ssl.wantClientAuth" default="false"/></Set> <Set name="ExcludeCipherSuites"> <Array type="String"> <Item>SSL_RSA_WITH_DES_CBC_SHA</Item> <Item>SSL_DHE_RSA_WITH_DES_CBC_SHA</Item> <Item>SSL_DHE_DSS_WITH_DES_CBC_SHA</Item> <Item>SSL_RSA_EXPORT_WITH_RC4_40_MD5</Item> <Item>SSL_RSA_EXPORT_WITH_DES40_CBC_SHA</Item> <Item>SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA</Item> <Item>SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA</Item> </Array> </Set> <!-- =========================================================== --> <!-- Create a TLS specific HttpConfiguration based on the --> <!-- common HttpConfiguration defined in jetty.xml --> <!-- Add a SecureRequestCustomizer to extract certificate and --> <!-- session information --> <!-- =========================================================== --> <New id="sslHttpConfig" class="org.eclipse.jetty.server.HttpConfiguration"> <Arg><Ref refid="httpConfig"/></Arg> <Call name="addCustomizer"> <Arg><New class="org.eclipse.jetty.server.SecureRequestCustomizer"/></Arg> </Call> </New> </Configure>
cargo插件发布到tomcat
要在setting.xml
中加上:
<pluginGroups> <pluginGroup>org.jboss.maven.plugins</pluginGroup> <pluginGroup>org.codehaus.cargo</pluginGroup> </pluginGroups>
-
standalone
模式,复制一个环境来部署。 -
existing
模式
<build> ... <plugins> ... <plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <version>1.0</version> <configuration> <container> <containerId>tomcat6x</containerId> <home>/opt/morganstudio/server/tomcat6</home> </container> <configuration> <type>standalone</type> <home>${project.build.directory}/tomcat6x</home> <properties> <cargo.servlet.port>8080</cargo.servlet.port> </properties> </configuration> </configuration> </plugin> ... </plugins> ... </build>
启动:mvn cargo:start
existing模式下指定发布到的服务器:
<plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <version>1.0</version> <configuration> <container> <containerId>tomcat6x</containerId> <home>/opt/morganstudio/server/tomcat6</home> </container> <configuration> <container> <container>tomcat6x</container> <home>/opt/apache-tomcat-6.0.29</home> </container> <configuration> <type>existing</type> <home>/opt/apache-tomcat-6.0.29</home> </configuration> </configuration> </configuration> </plugin>
部署到过程服务器
要有远程tomcat的管理员权限,container的type必须为remote,不然默认的是installed。 runtime表示即不是独立的也不是本地现有的,而是一个已经在运行中的容器。
<plugin> <groupId>org.codehaus.cargo</groupId> <artifactId>cargo-maven2-plugin</artifactId> <configuration> <container> <containerId>tomcat6x</containerId> <type>remote</type> </container> <configuration> <type>runtime</type> <properties> <cargo.remote.username>admin</cargo.remote.username> <cargo.remote.password>admin123</cargo.remote.password> <cargo.remote.manager.url>http://localhost:8080/manager</cargo.remote.manager.url> </properties> </configuration> </configuration> </plugin>
执行操作:mvn cargo:redeploy