maven installation path configuration, modify the library file path and the eclipse of configurations, create maven project.

Note: This article comes from: Yang Shiro 2018   " maven installation path configuration, modify the library file path and the eclipse of configurations, create maven project "

 

A, maven installation

First of all, first the official website to download maven. Here is the address of the official website: http://maven.apache.org/download.cgi   Please select Download the latest version:

Apache-maven-3.3.9-bin.zip to unzip under their own folder, such as extraction path: D: \ Program \ apache-maven-3.3.9.

Second, the path configuration


Right "Computer" and select "Properties", then click "Advanced System Settings", click on the "Environment Variables" to set environment variables, system variables need to configure the following:

New System Variable MAVEN_HOME variable value: D: \ Program \ apache-maven-3.3.9

Edit System Variable Path add the variable value:;% MAVEN_HOME% \ bin


If the last successful test configuration: a win key + R, to open the command prompt window, i.e. Dos interface, enter the following case mvn -version configuration was successful


Third, modify the library file path

maven as a very powerful organization and management tools, but it's the default repository directory on the C drive under the document, so if reinstall the computer will download jar package to eliminate all but permanent library after the accumulation of too much, likely to cause computer slow. For projects to redeploy although not difficult, but we

The warehouse can be done to move to another location, so that you can once and for all.

1, first open your own maven installation directory

Below find conf folder, open settings.xml file

Use ctrl + f to find localrepository words in the article, appears as shown below, this label will be changed to the path you want:

<localRepository>D:/Program Files/maven_repository/repository</localRepository>

创建你在settings.xml中指定真实仓库路径,并将修改后的settings.xml文件复制一份放在当前的目录下(原来的不要删除掉)

打开myeclipse点击windows->preferences->maven->User setting下修改user setting选项为:D:\Program Files\apache_maven_repository\settings.xml,并点击update settings。并点击下面的reindex按钮更新索引。

开始编译你的maven项目,那么下载的jar自动存放在你指定的目录下,如图所示:


四、eclipse中的配置

打开Eclipse,依次打开Windows-->Prefrences,点击Maven的右边的三角符号,以展开Maven的配置界面

点击Maven下面的Installations,出现如下界面,请点击Add

之后,出现如下界面,点击Directory,请选择你的Maven的安装目录,这里我的Maven的安装目录为

D:\Program\apache-maven-3.3.9  ,并点击确定.

最后,选择自己安装的maven,点击Apply,点击OK,即可完成


五、eclipse中创建maven工程


5.1不通过骨架创建maven的java工程


通过选择骨架创建maven工程,每次选择骨架时都需要联网下载,如果网络不通或者较慢的情况下会有很长时间的等待。

使用很是不方便,所以创建工程时可以不选择骨架直接创建工程。

打开Eclipse,创建Maven项目,依次点击File-->New-->Maven Project :


请选中Create a simple project(skip archetype selection),并可以选择自己的WorkSpace Location,之后点击Next

填写Group id和artifact id, Version默认,Packaging默认为jar,Name,Description选 填,其他的不填,之后点击Finish


Packaging:指定打包方式,默认为jar。选项有:jar、war、pom。

创建完成之后,查看以下界面,即MavenTest 项目。


5.2通过骨架创建maven的java工程


第一步:选择new→maven→Maven Project

第二步:next

第三步:next

选择maven的工程骨架,这里我们选择quickstart。

第四步:next

输入GroupId、ArtifactId、Version、Package信息点击finish完成。

非骨架创建和骨架创建Maven工程对比:

5.3 不使用骨架创建maven的web工程


第一步:不选用骨架

第二步:将打包方式选择为war

第三步:点击finish,工程创建成功。

第四步: 在工程中 /src/main/webapp下新建WEB-INF文件夹,然后在WEB-INF中创建web.xml:

web.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
     <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID" version="2.5">
         <welcome-file-list>
             <welcome-file>index.html</welcome-file>
             <welcome-file>index.htm</welcome-file>
             <welcome-file>index.jsp</welcome-file>
             <welcome-file>default.html</welcome-file>
             <welcome-file>default.htm</welcome-file>
             <welcome-file>default.jsp</welcome-file>
         </welcome-file-list>
 </web-app>


第五步:在webapp下创建index.jsp.

如图:


5.4使用骨架创建maven的web工程
1.Eclipse中用Maven创建项目


上图中Next


2.继续Next



3.选maven-archetype-webapp后,next



4.填写相应的信息,Packaged是默认创建一个包,不写也可以



5.创建好项目后,目录如下:


至此,项目已经创建完毕,下边可是配置。

非骨架创建和骨架创建Maven工程对比:

非骨架会出现错误信息:web.xml is missing and <failOnMissingWebXml> is set to true;

骨架创建可避免很多问题,web.xml和index.jsp自动生成。




二.项目配置

1.添加Source Folder

Maven规定,必须创建以下几个Source Folder

src/main/resources

src/main/java

src/test/resources

src/test/java

添加以上的Source Folder



创建好后的目录如下:


2.配置Build Path



3.设定4个文件夹的输出Output folder,双击修改


分别修改输出路径为

src/main/resources  对应  target/classes

src/main/java  对应  target/classes

src/test/resources  对应  target/test-classes

src/test/java  对应  target/test-classes

4.修改后如下图



5.设定Libraries




6.配置完Build Path后目录如下:


7.将项目转换成Dynamic Web Project

在项目上右键Properties

在左侧选择 Project Facets,单击右侧的 ”Convert faceted from “



8.修改Java为你当前项目的JDK,并添加Dynamic Web Module ,最后单击”Further Configuration available“ 链接:



9.修改Content directory 为 src/main/webapp ,单击OK:



10.设置完Content directory,ok后再次点击前一界面ok,完成转换成Dynamic Web Project项目



11.设置部署程序集(Web Deployment Assembly)

在项目上右键单击,选择Properties,在左侧选择Deployment Assembly



12.设置部署时的文件发布路径

  1,我们删除test的两项,因为test是测试使用,并不需要部署。
  2,设置将Maven的jar包发布到lib下。
    Add -> Java Build Path Entries -> Maven Dependencies -> Finish

设置完成后如图



ok后,web项目就创建完毕了,目录机构如图


运行后访问工程成功!


附上编译插件代码:

    <build>
         <plugins>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-compiler-plugin</artifactId>
                 <configuration>
                     <source>1.8</source>
                     <target>1.8</target>
                     <encoding>UTF-8</encoding>
                 </configuration>
             </plugin>
         </plugins>
     </build>

附上tomcat插件代码:

<plugin>
     <!-- 配置插件 -->
     <groupId>org.apache.tomcat.maven</groupId>
     <artifactId>tomcat7-maven-plugin</artifactId>
     <configuration>
         <port>8080</port>
         <path>/</path>
     </configuration>
</plugin>

---------------------
作者:杨四郎2018
来源:CSDN
原文:https://blog.csdn.net/yang5726685/article/details/53301968
版权声明:本文为博主原创文章,转载请附上博文链接!

Guess you like

Origin www.cnblogs.com/ios9/p/11199078.html