idea新建項目打包 ,運行jar,並放入maven倉庫


1.新建項目(轉自:http://www.cnblogs.com/wql025/p/5215570.html)

創建一個新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://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/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>
復制代碼

 

其他鏈接 

  如何構建多個子目錄,參考: http://www.cnblogs.com/acm-bingzi/p/6625202.html
  如果打成Jar包后報Unable to locate Spring NamespaceHandler for XML schema namespace錯,參考: http://www.cnblogs.com/acm-bingzi/p/6625123.html
  spring-boot-maven-plugin插件的作用,參考: http://www.cnblogs.com/acm-bingzi/p/mavenSpringBootPlugin.html

 

===================================================================================================================

3.將jar放入maven倉庫

 

命令如下:

mvn install:install-file -Dfile=c:\kaptcha-2.3.jar -DgroupId=com.google.code -DartifactId=kaptcha -Dversion=2.3 -Dpackaging=jar

mvn 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本地資源庫

(https://www.cnblogs.com/wangshouchang/p/6678512.html)

 

這里有2個案例,需要手動發出Maven命令包括一個 jar 到 Maven 的本地資源庫。

  1. 要使用的 jar 不存在於 Maven 的中心儲存庫中。
  2. 您創建了一個自定義的 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 本地存儲庫檢索了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM