• 创建一个Maven Web应用程序



    1 在Eclipes创建maven,首先File new ,在other 中找到Maven,Maven Project,下一步选择WebApp,创建Maven工程名字,完成


    2 在新建的Maven工程中,找到target,pom.xml,


    <build>
    <finalName>test6</finalName>  工程名字

    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
    <source>1.7</source>
    <target>1.7</target>   指定JDK
    </configuration>
    </plugin>
    </plugins>

    </build>

    3 在命名的工程中,修改JDK版本为指定或高于。在Java Buid Path , Edit 修改为Eclips配置的环境变量Jdk版本。


    4 找到Maven,Maven Update 更新工程。

    5  在target,pom.xml文件中添加依赖jar包,保存。

    <dependencies>
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    </dependency>
    <dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.3.1</version>
    </dependency>
    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
    </dependency>

    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.39</version>
    </dependency>
    </dependencies>

    6 在src/mian/webapp/ web.xml中修改servlet 版本

     7 在Project Facets 修改Server 版本



    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance

    "
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1" >
    </web-app>
    8 Maven, Maven Update 更新工程

    9 在src/mian/webapp/创建JSP File 文件,Run on Server 选择配置的好Tomcat版本运行WebApp程序。
    10 web.xml文件的作用:
     
    转载海山 博客

    每个javaEE工程中都有web.xml文件,那么它的作用是什么呢?它是每个web.xml工程都必须的吗?

    一个web中可以没有web.xml文件,也就是说,web.xml文件并不是web工程必须的。


    web.xml文件是用来初始化配置信息:比如Welcome页面、servlet、servlet-mapping、filter、listener、启动加载级别等。

    当你的web工程没用到这些时,你可以不用web.xml文件来配置你的Application。


    每个xml文件都有定义它书写规则的Schema文件,也就是说javaEE的定义web.xml所对应的xml Schema文件中定义了多少种标签元素,web.xml中就可以出现它所定义的标签元素,也就具备哪些特定的功能。web.xml的模式文件是由Sun 公司定义的,每个web.xml文件的根元素为<web-app>中,必须标明这个web.xml使用的是哪个模式文件。如:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    </web-app>

        web.xml的模式文件中定义的标签并不是定死的,模式文件也是可以改变的,一般来说,随着web.mxl模式文件的版本升级,里面定义的功能会越来越复杂,标签元素的种类肯定也会越来越多,但有些不是很常用的,我们只需记住一些常用的并知道怎么配置就可以了。


    下面列出web.xml我们常用的一些标签元素及其功能:

    1、指定欢迎页面,例如:
    <welcome-file-list>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>index1.jsp</welcome-file>
      </welcome-file-list>
    PS:指定了2个欢迎页面,显示时按顺序从第一个找起,如果第一个存在,就显示第一个,后面的不起作用。如果第一个不存在,就找第二个,以此类推。

    关于欢迎页面:

        访问一个网站时,默认看到的第一个页面就叫欢迎页,一般情况下是由首页来充当欢迎页的。一般情况下,我们会在web.xml中指定欢迎页。但 web.xml并不是一个Web的必要文件,没有web.xml,网站仍然是可以正常工作的。只不过网站的功能复杂起来后,web.xml的确有非常大用处,所以,默认创建的动态web工程在WEB-INF文件夹下面都有一个web.xml文件。


    2、命名与定制URL。我们可以为Servlet和JSP文件命名并定制URL,其中定制URL是依赖命名的,命名必须在定制URL前。下面拿serlet来举例:
    (1)、为Servlet命名:
    <servlet>
        <servlet-name>servlet1</servlet-name>
        <servlet-class>org.whatisjava.TestServlet</servlet-class>
    </servlet>

    (2)、为Servlet定制URL、
    <servlet-mapping>
        <servlet-name>servlet1</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>



    3、定制初始化参数:可以定制servlet、JSP、Context的初始化参数,然后可以再servlet、JSP、Context中获取这些参数值。

    下面用servlet来举例:
    <servlet>
        <servlet-name>servlet1</servlet-name>
        <servlet-class>org.whatisjava.TestServlet</servlet-class>
        <init-param>
              <param-name>userName</param-name>
              <param-value>Daniel</param-value>
        </init-param>
        <init-param>
              <param-name>E-mail</param-name>
              <param-value>125485762@qq.com</param-value>
        </init-param>
    </servlet>
    经过上面的配置,在servlet中能够调用getServletConfig().getInitParameter("param1")获得参数名对应的值。



    4、指定错误处理页面,可以通过“异常类型”或“错误码”来指定错误处理页面。
    <error-page>
        <error-code>404</error-code>
        <location>/error404.jsp</location>
    </error-page>
    -----------------------------
    <error-page>
        <exception-type>java.lang.Exception<exception-type>
        <location>/exception.jsp<location>
    </error-page>



    5、设置过滤器:比如设置一个编码过滤器,过滤所有资源
    <filter>
        <filter-name>XXXCharaSetFilter</filter-name>
        <filter-class>net.test.CharSetFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>XXXCharaSetFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>



    6、设置监听器:
    <listener>
    <listener-class>net.test.XXXLisenet</listener-class>
    </listener>



    7、设置会话(Session)过期时间,其中时间以分钟为单位,假如设置60分钟超时:
    <session-config>
    <session-timeout>60</session-timeout>
    </session-config>


    除了这些标签元素之外,还可以往web.xml中添加很多标签元素,由于不常用省略。

    转自:http://marshal.easymorse.com/archives/511

    11 什么是POM

    Project Object Model,项目对象模型。通过xml格式保存的pom.xml文件。作用类似ant的build.xml文件,功能更强大。该文件用于管理:源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的url、项目的依赖关系等等。

    一个完整的pom.xml文件,放置在项目的根目录下。

    <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/maven-v4_0_0.xsd"> 
      <modelVersion>4.0.0</modelVersion> 
      <!– The Basics –> 
      <groupId>…</groupId> 
      <artifactId>…</artifactId> 
      <version>…</version> 
      <packaging>…</packaging> 
      <dependencies>…</dependencies> 
      <parent>…</parent> 
      <dependencyManagement>…</dependencyManagement> 
      <modules>…</modules> 
      <properties>…</properties> 
      <!– Build Settings –> 
      <build>…</build> 
      <reporting>…</reporting> 
      <!– More Project Information –> 
      <name>…</name> 
      <description>…</description> 
      <url>…</url> 
      <inceptionYear>…</inceptionYear> 
      <licenses>…</licenses> 
      <organization>…</organization> 
      <developers>…</developers> 
      <contributors>…</contributors> 
      <!– Environment Settings –> 
      <issueManagement>…</issueManagement> 
      <ciManagement>…</ciManagement> 
      <mailingLists>…</mailingLists> 
      <scm>…</scm> 
      <prerequisites>…</prerequisites> 
      <repositories>…</repositories> 
      <pluginRepositories>…</pluginRepositories> 
      <distributionManagement>…</distributionManagement> 
      <profiles>…</profiles> 
    </project>

    基本设置

    maven的协作相关属性

    <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/maven-v4_0_0.xsd"> 
      <modelVersion>4.0.0</modelVersion> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>my-project</artifactId> 
      <version>1.0</version> 
      <packaging>war</packaging> 
    </project>

    1. groupId : 组织标识,例如:org.codehaus.mojo,在M2_REPO目录下,将是: org/codehaus/mojo目录。
    2. artifactId : 项目名称,例如:my-project,在M2_REPO目录下,将是:org/codehaus/mojo/my-project目录。
    3. version : 版本号,例如:1.0,在M2_REPO目录下,将是:org/codehaus/mojo/my-project/1.0目录。
    4. packaging : 打包的格式,可以为:pom , jar , maven-plugin , ejb , war , ear , rar , par
    POM之间的关系

    主要用于POM文件的复用。

    依赖关系:依赖关系列表(dependency list)是POM的重要部分。

      <dependencies> 
        <dependency> 
          <groupId>junit</groupId> 
          <artifactId>junit</artifactId> 
          <version>4.0</version> 
          <scope>test</scope> 
        </dependency> 
        … 
      </dependencies>

    1. groupId , artifactId , version :
    2. scope : compile(default),provided,runtime,test,system
    3. exclusions

    继承关系:继承其他pom.xml配置的机制。

    比如父pom.xml:

    <project> 
      [...] 
      <dependencies> 
        <dependency> 
          <groupId>junit</groupId> 
          <artifactId>junit</artifactId> 
          <version>4.4</version> 
          <scope>test</scope> 
        </dependency> 
      </dependencies> 
      [...] 
    </project>

    在子pom.xml文件继承它的依赖(还可以继承其他的:developers and contributors、plugin lists、reports lists、plugin executions with matching ids、plugin configuration):

    [...] 
    <parent> 
    <groupId>com.devzuz.mvnbook.proficio</groupId> 
      <artifactId>proficio</artifactId> 
      <version>1.0-SNAPSHOT</version> 
    </parent> 
    [...]

    在这种机制下,maven还提供了一个类似java.lang.Object的顶级父pom.xml文件:

    <project> 
      <modelVersion>4.0.0</modelVersion> 
      <name>Maven Default Project</name> 
      <repositories> 
        <repository> 
          <id>central</id> 
          <name>Maven Repository Switchboard</name> 
          <layout>default</layout> 
          <url>http://repo1.maven.org/maven2</url> 
          <snapshots> 
            <enabled>false</enabled> 
          </snapshots> 
        </repository> 
      </repositories> 
      <pluginRepositories> 
        <pluginRepository> 
          <id>central</id> 
          <name>Maven Plugin Repository</name> 
          <url>http://repo1.maven.org/maven2</url> 
          <layout>default</layout> 
          <snapshots> 
            <enabled>false</enabled> 
          </snapshots> 
          <releases> 
            <updatePolicy>never</updatePolicy> 
          </releases> 
        </pluginRepository> 
      </pluginRepositories> 
      <build> 
        <directory>target</directory> 
        <outputDirectory>target/classes</outputDirectory> 
        <finalName>${project.artifactId}-${project.version}</finalName> 
        <testOutputDirectory>target/test-classes</testOutputDirectory> 
        <sourceDirectory>src/main/java</sourceDirectory> 
        <scriptSourceDirectory>src/main/scripts</scriptSourceDirectory> 
        <testSourceDirectory>src/test/java</testSourceDirectory> 
        <resources> 
          <resource> 
            <directory>src/main/resources</directory> 
          </resource> 
        </resources> 
        <testResources> 
          <testResource> 
            <directory>src/test/resources</directory> 
          </testResource> 
        </testResources> 
        <pluginManagement> 
           <plugins> 
             <plugin> 
               <artifactId>maven-antrun-plugin</artifactId> 
               <version>1.1</version> 
             </plugin>       
             <plugin> 
               <artifactId>maven-assembly-plugin</artifactId> 
               <version>2.2-beta-2</version> 
             </plugin> 
             <plugin> 
               <artifactId>maven-clean-plugin</artifactId> 
               <version>2.2</version> 
             </plugin> 
             <plugin> 
               <artifactId>maven-compiler-plugin</artifactId> 
               <version>2.0.2</version> 
             </plugin> 
             <plugin> 
               <artifactId>maven-dependency-plugin</artifactId> 
               <version>2.0</version> 
             </plugin> 
             <plugin> 
               <artifactId>maven-deploy-plugin</artifactId> 
               <version>2.3</version> 
             </plugin> 
             <plugin> 
               <artifactId>maven-ear-plugin</artifactId> 
               <version>2.3.1</version> 
             </plugin> 
             <plugin> 
               <artifactId>maven-ejb-plugin</artifactId> 
               <version>2.1</version> 
             </plugin> 
             <plugin> 
               <artifactId>maven-install-plugin</artifactId> 
               <version>2.2</version> 
             </plugin> 
             <plugin> 
               <artifactId>maven-jar-plugin</artifactId> 
               <version>2.2</version> 
             </plugin> 
             <plugin> 
               <artifactId>maven-javadoc-plugin</artifactId> 
               <version>2.4</version> 
             </plugin> 
             <plugin> 
               <artifactId>maven-plugin-plugin</artifactId> 
               <version>2.4.1</version> 
             </plugin> 
             <plugin> 
               <artifactId>maven-rar-plugin</artifactId> 
               <version>2.2</version> 
             </plugin> 
             <plugin>                
               <artifactId>maven-release-plugin</artifactId> 
               <version>2.0-beta-7</version> 
             </plugin> 
             <plugin>                
               <artifactId>maven-resources-plugin</artifactId> 
               <version>2.2</version> 
             </plugin> 
             <plugin> 
               <artifactId>maven-site-plugin</artifactId> 
               <version>2.0-beta-6</version> 
             </plugin> 
             <plugin> 
               <artifactId>maven-source-plugin</artifactId> 
               <version>2.0.4</version> 
             </plugin>          
             <plugin> 
                <artifactId>maven-surefire-plugin</artifactId> 
                <version>2.4.2</version> 
             </plugin> 
             <plugin> 
               <artifactId>maven-war-plugin</artifactId> 
               <version>2.1-alpha-1</version> 
             </plugin> 
           </plugins> 
         </pluginManagement> 
      </build> 
      <reporting> 
        <outputDirectory>target/site</outputDirectory> 
      </reporting> 
      <profiles> 
        <profile> 
          <id>release-profile</id> 
          <activation> 
            <property> 
              <name>performRelease</name> 
              <value>true</value> 
            </property> 
          </activation> 
          <build> 
            <plugins> 
              <plugin> 
                <inherited>true</inherited> 
                <groupId>org.apache.maven.plugins</groupId> 
                <artifactId>maven-source-plugin</artifactId> 
                <executions> 
                  <execution> 
                    <id>attach-sources</id> 
                    <goals> 
                      <goal>jar</goal> 
                    </goals> 
                  </execution> 
                </executions> 
              </plugin> 
              <plugin> 
                <inherited>true</inherited> 
                <groupId>org.apache.maven.plugins</groupId> 
                <artifactId>maven-javadoc-plugin</artifactId> 
                <executions> 
                  <execution> 
                    <id>attach-javadocs</id> 
                    <goals> 
                      <goal>jar</goal> 
                    </goals> 
                  </execution> 
                </executions> 
              </plugin> 
              <plugin> 
                <inherited>true</inherited> 
                <groupId>org.apache.maven.plugins</groupId> 
                <artifactId>maven-deploy-plugin</artifactId> 
                <configuration> 
                  <updateReleaseInfo>true</updateReleaseInfo> 
                </configuration> 
              </plugin> 
            </plugins> 
          </build> 
        </profile> 
      </profiles> 
    </project>

    可以通过下面命令查看当前pom.xml受到超pom.xml文件的影响:

    mvn help:effective-pom

    聚合关系:用于将多个maven项目聚合为一个大的项目。

    <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/maven-v4_0_0.xsd"> 
      <modelVersion>4.0.0</modelVersion> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>my-parent</artifactId> 
      <version>2.0</version> 
      <modules> 
        <module>my-project<module> 
      </modules> 
    </project>

    属性

    maven的属性,是值的占位符,类似EL,类似ant的属性,比如${X},可用于pom文件任何赋值的位置。有以下分类:

    1. env.X:操作系统环境变量,比如${env.PATH}
    2. project.x:pom文件中的属性,比如:<project><version>1.0</version></project>,引用方式:${project.version}
    3. settings.x:settings.xml文件中的属性,比如:<settings><offline>false</offline></settings>,引用方式:${settings.offline}
    4. Java System Properties:java.lang.System.getProperties()中的属性,比如java.home,引用方式:${java.home}
    5. 自定义:在pom文件中可以:<properties><installDir>c:/apps/cargo-installs</installDir></properties>,引用方式:${installDir}

    构建设置

    构建有两种build标签:

    <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/maven-v4_0_0.xsd"> 
      … 
      <!– "Project Build" contains more elements than just the BaseBuild set –> 
      <build>…</build> 
      <profiles> 
        <profile> 
          <!– "Profile Build" contains a subset of "Project Build"s elements –> 
          <build>…</build> 
        </profile> 
      </profiles> 
    </project>

    build中的主要标签:Resources和Plugins。

    Resources:用于排除或包含某些资源文件

        <resources> 
          <resource> 
            <targetPath>META-INF/plexus</targetPath> 
            <filtering>false</filtering> 
            <directory>${basedir}/src/main/plexus</directory> 
            <includes> 
              <include>configuration.xml</include> 
            </includes> 
            <excludes> 
              <exclude>**/*.properties</exclude> 
            </excludes> 
          </resource> 
        </resources>

    Plugins:设置构建的插件

      <build> 
        … 
        <plugins> 
          <plugin> 
            <groupId>org.apache.maven.plugins</groupId> 
            <artifactId>maven-jar-plugin</artifactId> 
            <version>2.0</version> 
            <extensions>false</extensions> 
            <inherited>true</inherited> 
            <configuration> 
              <classifier>test</classifier> 
            </configuration> 
            <dependencies>…</dependencies> 
            <executions>…</executions> 
          </plugin>

  • 相关阅读:
    Rman备份及不完全恢复操作
    win2003系统同步Linux ntp server批处理
    ntp服务器搭建
    notepad++调用python3中文乱码
    10G安装DataGuard
    oracle安装配置
    python之路(14)进程
    python之路(13)线程
    python之路(12)网络编程
    python之路(11)描述符
  • 原文地址:https://www.cnblogs.com/TangGe520/p/8588572.html
Copyright © 2020-2023  润新知