之前碰到过很多javaWeb工程,直接使用lib文件夹管理jar包,缺点多多,jar包版本混乱,不堪其扰,故创建pom、整理成maven管理。

    而且这种工程打包基本都是使用eclipse等开发工具打包,不方便使用自动构建工具,使用maven管理可以使用它的打包工具。

    1. 创建pom文件,将配置信息和依赖jar包信息写入其中
      jar包相关信息可以去该网址去查找。
    1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    3. <modelVersion>4.0.0</modelVersion>
    4. <groupId>com.chongtong</groupId>
    5. <artifactId>demo</artifactId>
    6. <version>0.0.1-SNAPSHOT</version>
    7. <name>demo</name>
    8. <description>demo</description>
    9. <properties>
    10. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    11. <jdk.version>1.8</jdk.version>
    12. <spring.version>3.0.5.RELEASE</spring.version>
    13. </properties>
    14. <dependencies>
    15. <!-- core -->
    16. <dependency>
    17. <groupId>org.springframework</groupId>
    18. <artifactId>spring-context</artifactId>
    19. <version>${spring.version}</version>
    20. </dependency>
    21. <dependency>
    22. <groupId>org.springframework</groupId>
    23. <artifactId>spring-context-support</artifactId>
    24. <version>${spring.version}</version>
    25. </dependency>
    26. <dependency>
    27. <groupId>org.springframework</groupId>
    28. <artifactId>spring-core</artifactId>
    29. <version>${spring.version}</version>
    30. </dependency>
    31. <dependency>
    32. <groupId>org.springframework</groupId>
    33. <artifactId>spring-beans</artifactId>
    34. <version>${spring.version}</version>
    35. </dependency>
    36. <!-- web -->
    37. <dependency>
    38. <groupId>org.springframework</groupId>
    39. <artifactId>spring-web</artifactId>
    40. <version>${spring.version}</version>
    41. </dependency>
    42. <dependency>
    43. <groupId>org.springframework</groupId>
    44. <artifactId>spring-servlet</artifactId>
    45. <version>${spring.version}</version>
    46. </dependency>
    47. </dependencies>
    48. <build>
    49. <finalName>demo</finalName>
    50. <resources>
    51. <resource>
    52. <directory>src/main/resources</directory>
    53. <includes>
    54. <include>**/*.properties</include>
    55. <include>**/*.xml</include>
    56. </includes>
    57. <filtering>false</filtering>
    58. </resource>
    59. <resource>
    60. <directory>src/main/java</directory>
    61. <includes>
    62. <include>**/*.properties</include>
    63. <include>**/*.xml</include>
    64. </includes>
    65. <filtering>false</filtering>
    66. </resource>
    67. </resources>
    68. <plugins>
    69. <plugin>
    70. <groupId>org.apache.maven.plugins</groupId>
    71. <artifactId>maven-compiler-plugin</artifactId>
    72. <version>3.6.2</version>
    73. <configuration>
    74. <source>${jdk.version}</source>
    75. <target>${jdk.version}</target>
    76. <encoding>utf8</encoding>
    77. </configuration>
    78. </plugin>
    79. <!-- The configuration of maven-assembly-plugin -->
    80. <plugin>
    81. <groupId>org.apache.maven.plugins</groupId>
    82. <artifactId>maven-assembly-plugin</artifactId>
    83. <version>3.1.0</version>
    84. <!-- The configuration of the plugin -->
    85. <configuration>
    86. <!-- Specifies the configuration file of the assembly plugin -->
    87. <descriptors>
    88. <descriptor>src/main/assembly/package.xml</descriptor>
    89. </descriptors>
    90. <!-- 设为 FALSE, 防止 WAR 包名加入 assembly.xml 中的 ID -->
    91. <appendAssemblyId>false</appendAssemblyId>
    92. </configuration>
    93. <executions>
    94. <execution>
    95. <id>make-assembly</id>
    96. <phase>package</phase>
    97. <goals>
    98. <goal>single</goal>
    99. </goals>
    100. </execution>
    101. </executions>
    102. </plugin>
    103. <plugin>
    104. <artifactId>maven-war-plugin</artifactId>
    105. <version>2.6</version>
    106. <configuration>
    107. <warSourceDirectory>${basedir}/src/main/webapp
    108. </warSourceDirectory>
    109. <webXml>${basedir}/src/main/webapp/WEB-INF/web.xml</webXml>
    110. </configuration>
    111. </plugin>
    112. <plugin>
    113. <groupId>org.codehaus.mojo</groupId>
    114. <artifactId>findbugs-maven-plugin</artifactId>
    115. <version>2.3</version>
    116. <configuration>
    117. <findbugsXmlOutput>true</findbugsXmlOutput>
    118. <xmlOutput>true</xmlOutput>
    119. </configuration>
    120. </plugin>
    121. </plugins>
    122. </build>
    123. <packaging>war</packaging>
    124. </project>
    1. 创建打包文件package.xml,将打包策略写入其中
    1. <assembly>
    2. <id>${project.artifactId}-assembly-${project.version}</id>
    3. <!-- 默认为 TRUE, 设为 FALSE, 防止将 ${project.finalName} 作为根目录打进 WAR 包 -->
    4. <!-- TRUE 结构: ${project.finalName}.war/${project.finalName}/WEB-INF -->
    5. <!-- FALSE 结构: ${project.finalName}.war/WEB-INF -->
    6. <includeBaseDirectory>false</includeBaseDirectory>
    7. <!-- 设置为 WAR 包格式 -->
    8. <formats>
    9. <format>war</format>
    10. </formats>
    11. <fileSets>
    12. <!-- 将 target/classes 下的文件输出到 WEB-INF/classes -->
    13. <fileSet>
    14. <directory>${project.build.outputDirectory}</directory>
    15. <outputDirectory>WEB-INF/classes</outputDirectory>
    16. </fileSet>
    17. <!-- 将 webapp 下的文件输出到 WAR 包 -->
    18. <fileSet>
    19. <directory>${project.basedir}/src/main/webapp</directory>
    20. <outputDirectory>/</outputDirectory>
    21. </fileSet>
    22. </fileSets>
    23. <!-- 将项目依赖的JAR包输出到 WEB-INF/lib -->
    24. <dependencySets>
    25. <dependencySet>
    26. <outputDirectory>WEB-INF/lib</outputDirectory>
    27. <excludes>
    28. <exclude>${project.groupId}:${project.artifactId}:war</exclude>
    29. </excludes>
    30. </dependencySet>
    31. </dependencySets>
    32. </assembly>