1.新建项目(转自:)
创建一个新Maven项目
- new 一个project

- 不选择任何Maven模板

- 起个GroupId、ArifactId

- 起个项目名。注意:Idea_Project是存放此项目的工作区间,mavenDemo_idea15为存放此项目的子目录。

- 建好项目后,打开,点击Auto-Import

- 下面为此项目的结构

项目部署
- 点击

Project: 无需设置 (当然可点击Project complier output自定义编译目录)

Modules:可看到此项目无任何适配服务组件(因为是手工创建Maven,没有选择任何Maven模板)--因此需要我们进行添加。

- 选择Web(为此项目添加Web服务组件,这便是一个Web项目了)

- 现在为Web设置资源目录。双击Web Resource Directory

- 选择scr/main目录

- 在后面加上webapp。好了,点OK,Web的资源目录便设置好了。

- 现在设置Web的描述文件的目录

- 设置在webapp目录下

Facts: 表示当前项目的适配服务组件。可看到此项目已是一个Web项目了。

Aftifacts: 这个Aftifacts描述了当前项目发布的信息。现在进行添加,从Modeles中选择。


说明:A: 现在Artifacts已有了发布的项目了(idea中准确的说应是Modele) B:output root目录描述了当前项目的编译目录及适配服务。

确定之后当前项目的结构:

- 如有需要,添加lib包


部署服务器

- 添加服务器

- 部署
注:很多童鞋在这里找不到Arifact,请参考部署项目中的Modules的配置。如果没有为项目配置Web服务组件,那么就没有Artifact。(当前项目连Web项目都不是,哪儿来的Artifact,又部署什么呢?)


- 注意下面的选择:

编写代码测试
- 创建一个java类。可看到继承HttpServlet出问题了--这是因为没有把Tomcat的依赖加入Module

- 在Modules加入Tomcat依赖



添加完毕

- 现在按快捷键就可以了

- 代码编辑
Java
package com.wql;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Created by Lenovo on 2016/2/25.
*/
@WebServlet("/myController")
public class Controller extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// System.err.println("---");
//解决乱码
req.setCharacterEncoding("UTF-8");
String name=req.getParameter("name");
req.setAttribute("name",name);
System.out.println(name);
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
}
Html
<%--
Created by IntelliJ IDEA.
User: Lenovo
Date: 2016/2/25
Time: 0:26
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="myController" method="post">
<input name="name">
return:${name}
<input value="提交" type="submit">
</form>
</body>
</html>
Xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_0.xsd"
version="3.0">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
- 记得设置默认启动浏览器

- 启动项目

===================================================================================================================
2.项目打包
最简单的方法
首先是在maven项目的pom.xml中添加打包的插件,这里有很多种方式的。最最简单的就是只使用maven-compiler-plugin、maven-jar-plugin插件,并且指定程序入口<mainClass>。相关代码如下:
pom.xml文件为:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http:///POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http:///POM/4.0.0 http:///xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.mymaven</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<useUniqueVersions>false</useUniqueVersions>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>cn.mymaven.test.TestMain</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
入口类TestMain.java为:

package cn.mymaven.test;
public class TestMain {
public static void main(String[] args){
System.out.println("Hello World");
}
}
然后开始打包,在Idea中把Maven项目的命令都做成了可视化的操作界面,只需要如下操作就好:

在Maven Project目录下,点击package

此时在target目录下,就会生成这个项目的Jar包

使用java -jar 命令运行这个Jar包,会输出“Hello World”
需要注意的地方
需要说明的是,如果一个maven项目中有多个子目录,每一个子目录中的pom.xml对应一个项目,它的作用范围只有这一个子目录下的。比如扫描配置文件,如果要让一个子目录下的pom.xml扫描另一个子目录下的配置文件,那是做不到的。在打jar包的时候,只运行当前的pom.xml文件。
当然也有其他的打包方法,比如使用spring-boot-maven-plugin插件在打Jar包时,会引入依赖包。
它的pom.xml文件配置为:

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<useUniqueVersions>false</useUniqueVersions>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>cn.mymaven.test.TestMain</mainClass>
</manifest>
<manifestEntries>
<version>${project.version}</version>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
其他链接
如何构建多个子目录,参考:
如果打成Jar包后报Unable to locate Spring NamespaceHandler for XML schema namespace错,参考:
spring-boot-maven-plugin插件的作用,参考:
===================================================================================================================
3.将jar放入maven仓库
命令如下:
mvn install:install-file -Dfile=c:\kaptcha-2.3.jar -DgroupId=com.google.code -DartifactId=kaptcha -Dversion=2.3 -Dpackaging=jarmvn install:install-file -Dfile= C:\Users\Administrator\IdeaProjects\demo2\target\demo2-1.0-SNAPSHOT.jar -DgroupId=com.google.code -DartifactId=kaptcha -Dversion=2.3 -Dpackaging=jar
例子如下:
自己把jar包添加到maven仓库中
定制库到Maven本地资源库
()
这里有2个案例,需要手动发出Maven命令包括一个 jar 到 Maven 的本地资源库。
- 要使用的 jar 不存在于 Maven 的中心储存库中。
- 您创建了一个自定义的 jar ,而另一个 Maven 项目需要使用。
PS,还是有很多 jar 不支持 Maven 的。
案例学习
例如,kaptcha,它是一个流行的第三方Java库,它被用来生成 “验证码” 的图片,以阻止垃圾邮件,但它不在 Maven 的中央仓库中。
在本教程中,我们将告诉你如何安装 “kaptcha” jar 到Maven 的本地资源库。
1. mvn 安装
下载 “kaptcha”,将其解压缩并将 kaptcha-version.jar 复制到其他地方,比如:C盘。发出下面的命令:
mvn install:install-file -Dfile=c:\kaptcha-{version}.jar -DgroupId=com.google.code -DartifactId=kaptcha -Dversion={version} -Dpackaging=jar示例:
(mvn命令建议在cmd窗口中运行)
D:\>mvn install:install-file -Dfile=c:\kaptcha-2.3.jar -DgroupId=com.google.code -DartifactId=kaptcha -Dversion=2.3 -Dpackaging=jar
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'install'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO] task-segment: [install:install-file] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] [install:install-file]
[INFO] Installing c:\kaptcha-2.3.jar to
D:\maven_repo\com\google\code\kaptcha\2.3\kaptcha-2.3.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: < 1 second
[INFO] Finished at: Tue May 12 13:41:42 SGT 2014
[INFO] Final Memory: 3M/6M
[INFO] ------------------------------------------------------------------------
现在,“kaptcha” jar被复制到 Maven 本地存储库。
2. pom.xml
安装完毕后,就在 pom.xml 中声明 kaptcha 的坐标。
<dependency>
<groupId>com.google.code</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3</version>
</dependency>3. 完成
构建它,现在 “kaptcha” jar 能够从你的 Maven 本地存储库检索了。
















