mac下安装maven

这里下载maven,解压到文件夹,比如/User/tao/dev

配置环境变量:vim ~/.bash_profile

填写以下内容:

export M2_HOME=/Users/tao/dev/apache-maven-3.5.2
export PATH=$PATH:$M2_HOME/bin

查看maven版本:mvn -v

Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T15:58:13+08:00)
Maven home: /Users/tao/dev/apache-maven-3.5.2
Java version: 1.7.0_45, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.13", arch: "x86_64", family: "mac"

maven打包和创建war工程

mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-webapp -DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false -DgroupId=com.waylau -DartifactId=simple-service-webapp -Dpackage=com.waylau.rest -DarchetypeVersion=2.16

这个就是从远程下载一个archetype(骨架),通过命令mvn archetype:generate可以列出所有的远程archetype,估计有950个。http://garyli.iteye.com/blog/1717384

创建一个工程名为simple-service-webapp(文件夹)的工程,里面有子文件夹:srcpom.xml文件。

打包:mvn clean package

产生新的文件夹或文件:

+ classes
+ generated-sources
+ maven-archiver
- simple-service-webapp
  + META-INF
  + WEB-INF
  index.jsp
- simple-service-webapp.war

将war包放到servlet容器下,访问:http://localhost:8080/simple-service-webapp/

则出现页面:

Jersey RESTful Web Application!

Jersey resource
Visit Project Jersey website for more information on Jersey!

点击Jersey resource,显示页面:Got it!

脱离IDE,mvn加网络构建一个普通maven工程

1.在线创建maven普通工程
mvn archetype:generate,然后输入maven-archetype-quickstart,接下来填写groupId/artifactId/version/package等信息,则生成了一个简单的骨架,包括main/test的代码包,以及引入了jUnit,代码包里生成有App.java/TestApp.java

2.编译mvn compile构建形成target目录,测试mvn test跑jUnit测试类

3.现在想在mvn里跑一个Main类,方法如下:

 <profiles>
  <profile>
   <id>start-server</id>
   <build>
    <plugins>
     <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <version>1.1.1</version>
      <executions>
       <execution>
        <phase>test</phase>
        <goals>
         <goal>java</goal>
        </goals>
        <configuration>
         <mainClass>de.tao.App</mainClass>
        </configuration>
       </execution>
      </executions>
     </plugin>
    </plugins>
   </build>
  </profile>
 </profiles>

这个时候运行mvn test -Pstart-server即运行了Main方法,不过是依顺序执行的,如果test没跑过出现了异常,则中断了。

更多方法参考这里

在Main类里启服务

添加其它依赖

<dependency>
   <groupId>org.glassfish.jersey.containers</groupId>
   <artifactId>jersey-container-jdk-http</artifactId>
   <version></version>
  </dependency>

添加以下几个类:

(1)服务类注册

package de.tao;

import org.glassfish.jersey.server.ResourceConfig;

public class RestApplication extends ResourceConfig {
        public RestApplication() {
                // 资源类所在的包路径
                packages("de.tao.resources");
                // 注册 MultiPart
//              register(MultiPartFeature.class);
        }
}

(2)服务类

package de.tao.resources;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;


/**
 * Root resource (exposed at "myresource" path)
 */
@Path("myresource")
public class MyResource {

        /**
         * Method handling HTTP GET requests. The returned object will be sent to
         * the client as "text/plain" media type.
         *
         * @return String that will be returned as a text/plain response.
         */
        @GET
        @Produces(MediaType.TEXT_PLAIN)
        public String getIt() {
                return "Got it!";
        }
}

(3)修改App.java

package de.tao;
import java.net.URI;
import org.glassfish.jersey.jdkhttp.JdkHttpServerFactory;

/**
 * Hello world!
 *
 */
public class App
{
    public static final String BASE_URI = "http://localhost:8081/";
    
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        JdkHttpServerFactory.createHttpServer(URI.create(BASE_URI), new RestApplication());
    }

    public static String say() {
        return "Hello";
    }
}

这时候运行mvn test -Pstart-server就将启动一个RESTful服务,访问http://localhost:8081/myresource得到输出Got it!