一步一步做项目(16)使项目支持maven

一步一步做项目(16)使项目支持maven

在前面一步一步做项目(15)用户登录的基础上进行,也可以在前面完成的任何步骤的基础上进行,也可以绕过这一步。

转变项目支持maven

在项目浏览器中,在项目节点上单击右键,弹出全局菜单,选择Configure->Convert to Maven Project,将项目转换为提供maven支持,如下图所示:
转换maven支持

pom.xml

在项目根目录下创建pom.xml配置文件,进行项目配置,要提供SSH支持,可配置如下:

<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>cn.lut.curiezhang</groupId>
  <artifactId>cmis</artifactId>
  <name>CurieMIS</name>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>war</packaging>
  <properties>
    <springframework.version>5.2.0.RELEASE</springframework.version>
    <struts.version>2.5.20</struts.version>
    <hibernate.version>5.4.7.Final</hibernate.version>
    <spring.plugins.version>3.2.2.RELEASE</spring.plugins.version>
    <junit.version>4.12</junit.version>
    <junit-jupiter.version>5.5.2</junit-jupiter.version>
    <maven-clean-plugin.version>3.1.0</maven-clean-plugin.version>
    <maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
    <maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
    <maven-resources-plugin.version>3.1.0</maven-resources-plugin.version>
    <maven-war-plugin.version>3.2.3</maven-war-plugin.version>
    <jetty-maven-plugin.version>9.4.21.v20190926</jetty-maven-plugin.version>
    <encoding.code>UTF-8</encoding.code>
    <java.source.version>1.8</java.source.version>
    <java.target.version>1.8</java.target.version>
    <javax.servlet-api.version>4.0.1</javax.servlet-api.version>
    <bcprov-jdk15on.version>1.64</bcprov-jdk15on.version>
    <mysql.version>8.0.18</mysql.version>
    <log4j-core.version>2.11.1</log4j-core.version>
  </properties>

  <dependencies>
		<!-- asm-all -->
		<dependency>
		    <groupId>org.ow2.asm</groupId>
		    <artifactId>asm-all</artifactId>
		    <version>5.2</version>
		</dependency>
		<!-- commons-logging -->
		<dependency>
		    <groupId>commons-logging</groupId>
		    <artifactId>commons-logging</artifactId>
		    <version>1.2</version>
		</dependency>
    <!-- logging -->
		<dependency>
		    <groupId>org.apache.logging.log4j</groupId>
		    <artifactId>log4j-core</artifactId>
		    <version>${log4j-core.version}</version>
		</dependency>
		<!-- MySQL -->
		<dependency>
		    <groupId>mysql</groupId>
		    <artifactId>mysql-connector-java</artifactId>
		    <version>${mysql.version}</version>
		</dependency>
    <!-- javax.servlet.http -->
		<dependency>
		    <groupId>javax.servlet</groupId>
		    <artifactId>javax.servlet-api</artifactId>
		    <version>${javax.servlet-api.version}</version>
		    <scope>provided</scope>
		</dependency>
    <!-- 加密解密 -->
		<dependency>
		    <groupId>org.bouncycastle</groupId>
		    <artifactId>bcprov-jdk15on</artifactId>
		    <version>${bcprov-jdk15on.version}</version>
		</dependency>
		<dependency>
		    <groupId>org.bouncycastle</groupId>
		    <artifactId>bcprov-ext-jdk15on</artifactId>
		    <version>${bcprov-jdk15on.version}</version>
		</dependency>
		<!-- struts2 -->
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-core</artifactId>
        <version>${struts.version}</version>
    </dependency>
		<!-- struts2-spring-plugin -->
		<dependency>
		    <groupId>org.apache.struts</groupId>
		    <artifactId>struts2-spring-plugin</artifactId>
		    <version>${struts.version}</version>
		</dependency>
    <!-- hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
		<!-- hibernate-c3p0 -->
		<dependency>
		    <groupId>org.hibernate</groupId>
		    <artifactId>hibernate-c3p0</artifactId>
		    <version>${hibernate.version}</version>
		</dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>${springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>${junit-jupiter.version}</version>
      <scope>test</scope>
    </dependency>
    <!-- <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency> -->
  </dependencies>

    <repositories>
        <repository>
            <id>aliyun</id>
            <name>aliyun</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
        </repository>
    </repositories>

  <build>
  	<defaultGoal>install</defaultGoal>
    <!-- 打成war的最终名 -->
    <finalName>cmis</finalName>
    <!-- 指定java源代码存放的目录 -->
    <sourceDirectory>src</sourceDirectory>
    <outputDirectory>target\classes</outputDirectory>
    <testOutputDirectory>target\test-classes</testOutputDirectory>
    <!-- 指定资源文件存放的目录 -->
    <resources>
      <resource>
        <directory>src</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
    <!-- 指定测试源代码存放的目录 -->
    <testSourceDirectory>test</testSourceDirectory>
    <!-- 指定测试资源文件存放的目录 -->
    <testResources>  
      <testResource>  
        <directory>test</directory>  
      </testResource>  
    </testResources>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-clean-plugin</artifactId>
          <version>${maven-clean-plugin.version}</version>
        </plugin>
        <!-- 编译插件 -->
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>${maven-compiler-plugin.version}</version>
          <configuration>
            <source>${java.source.version}</source>
            <target>${java.target.version}</target>
          </configuration>
        </plugin>
				<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-war-plugin -->
				<plugin>
			    <groupId>org.apache.maven.plugins</groupId>
			    <artifactId>maven-war-plugin</artifactId>
			    <version>${maven-war-plugin.version}</version>
					<configuration>
						<webappDirectory>
							Z:/_Server/apache-tomcat-9.0.14/webapps/cmis
						</webappDirectory>
						<warSourceDirectory>
							WebContent
						</warSourceDirectory>
						<webResources>
							<resource>
								<directory>WebContent\WEB-INF</directory>
								<targetPath>WEB-INF</targetPath>
								<filtering>true</filtering>
								<includes>
									<include>web.xml</include>
								</includes>
							</resource>
						</webResources>
					</configuration>
				</plugin>
        <!-- <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>${maven-surefire-plugin.version}</version>
        </plugin> -->
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-resources-plugin</artifactId>
          <version>${maven-resources-plugin.version}</version>
          <configuration>
            <encoding>${encoding.code}</encoding>
          </configuration>
        </plugin>
        <!-- web容器插件 -->
        <plugin>
          <groupId>org.eclipse.jetty</groupId>
          <artifactId>jetty-maven-plugin</artifactId>
          <version>${jetty-maven-plugin.version}</version>
          <configuration>
            <war>target/cmis.war</war>
		    <httpConnector>
		        <port>8080</port>
		        <host>localhost</host>
		    </httpConnector>
            <!-- <scanIntervalSeconds>10</scanIntervalSeconds> -->
            <webAppSourceDirectory>
              WebContent
            </webAppSourceDirectory>
            <webApp>
              <contextPath>/cmis</contextPath>
            </webApp>
            <classesDirectory>target/classes</classesDirectory>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>

</project>

提供配置之后,就可以使用maven来进行工作了,当然,要没有错误才可以噢。
现在就可以将原来导入的lib移除,改为maven管理了。

使用maven

在项目浏览器中,在项目节点上单击右键,弹出全局菜单,选择Run As->Maven install或其他maven命令,如下图所示:

使用maven
选择Run As->Maven build…,在Goals中输入jetty:run-war或jetty:run(相关的配置参考jetty-maven-plugin),就可以启动jetty服务器进行软件部署和测试,如下图所示:

jetty:run-war
系统将使用jetty服务器来部署web应用,下面是登录的截图:
登录
应用经测试能够正常运行,环境不同,可能要进行不同的调整。

发布了42 篇原创文章 · 获赞 15 · 访问量 5864

猜你喜欢

转载自blog.csdn.net/ZhangCurie/article/details/102707976