构建Maven项目并上传到码云gitee【一篇就够】

一、构建Java项目

create is deprecated in maven 3.0.5 and beyond,在maven3.0.5以上版本舍弃了create,使用generate生成项目

mvn archetype:generate -DgroupId=com.sjmz.javademo -DartifactId=javademo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

C:\20170801\GitProject>mvn archetype:generate -DgroupId=com.sjmz.javademo -DartifactId=javademo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
[INFO] Scanning for projects...
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-antrun-plugin/1.3/maven-antrun-plugin-1.3.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-antrun-plugin/1.3/maven-antrun-plugin-1.3.jar (24 kB at 6.9 kB/s)
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:3.0.1:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:3.0.1:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO]
[INFO] --- maven-archetype-plugin:3.0.1:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Batch mode
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: C:\20170801\GitProject
[INFO] Parameter: package, Value: com.sjmz.javademo
[INFO] Parameter: groupId, Value: com.sjmz.javademo
[INFO] Parameter: artifactId, Value: javademo
[INFO] Parameter: packageName, Value: com.sjmz.javademo
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: C:\20170801\GitProject\javademo
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 09:00 min
[INFO] Finished at: 2019-01-02T22:32:14+08:00
[INFO] Final Memory: 15M/164M
[INFO] ------------------------------------------------------------------------

C:\20170801\GitProject>

显示BUILD SUCCESS即表示构建项目成功!!!

构建好的java项目目录结构如下:

构建Maven项目并上传到码云gitee【一篇就够】_第1张图片

开发目录src/main/java/com/sjmz/javademo下,创建了一个默认的App类文件,内容如下:

package com.sjmz.javademo;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
    }
}

测试目录src/test/java/com/sjmz/javademo下,创建了一个默认的AppTest类文件,内容如下:

package com.sjmz.javademo;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for simple App.
 */
public class AppTest 
    extends TestCase
{
    /**
     * Create the test case
     *
     * @param testName name of the test case
     */
    public AppTest( String testName )
    {
        super( testName );
    }

    /**
     * @return the suite of tests being tested
     */
    public static Test suite()
    {
        return new TestSuite( AppTest.class );
    }

    /**
     * Rigourous Test :-)
     */
    public void testApp()
    {
        assertTrue( true );
    }
}

实际项目开发中,我们一般都会有配置文件,例如log4j.properties,所以我们还需要手动创建src/main/resources(存放项目开发中用到的配置文件)和src/test/resources(存放测试时用到的配置文件),如下:

构建Maven项目并上传到码云gitee【一篇就够】_第2张图片

二、项目中pom文件的说明

通过Maven构建的项目,在项目根目录下都会有一个pom.xml文件,这个文件是Maven的核心。

构建Maven项目并上传到码云gitee【一篇就够】_第3张图片

1)pom意思就是project object model
2)pom.xml包含了项目构建的信息,包括项目的信息、项目的依赖等
3)pom.xml文件是可以继承的,大型项目中,子模块的pom.xml一般都会继承于父模块的pom.xml

pom.xml的内容如下:


  4.0.0
  com.sjmz.javademo
  javademo
  jar
  1.0-SNAPSHOT
  javademo
  http://maven.apache.org
  
    
      junit
      junit
      3.8.1
      test
    
  

pom.xml文件的节点元素说明:
           pom文件的顶级节点
        object model版本,对Maven2和Maven3来说,只能是4.0.0 
        项目创建组织的标识符,一般是域名的倒写
        定义了项目在所属组织的标识符下的唯一标识,一个组织下可以有多个项目
        当前项目的版本,SNAPSHOT,表示是快照版本,在开发中
        打包的方式,有jar、war、ear等
            项目的名称
            项目的地址
        属性配置,比如:UTF-8
        构建项目依赖的jar

其中由groupId、artifactId和version唯一的确定了一个项目坐标。

三、编译-测试-打包-安装-运行

1、编译

进入javademo目录,运行mvn clean compile

C:\20170801\GitProject>cd javademo

C:\20170801\GitProject\javademo>mvn clean compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building javademo 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ javademo ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ javademo ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ javademo ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\20170801\GitProject\javademo\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.441 s
[INFO] Finished at: 2019-01-02T22:58:20+08:00
[INFO] Final Memory: 12M/118M
[INFO] ------------------------------------------------------------------------

C:\20170801\GitProject\javademo>

编译会多出一个target目录,classes文件夹里存放的就是编译后的class文件。

构建Maven项目并上传到码云gitee【一篇就够】_第4张图片

2、测试

运行mvn clean test

C:\20170801\GitProject\javademo>mvn clean test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building javademo 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ javademo ---
[INFO] Deleting C:\20170801\GitProject\javademo\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ javademo ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform depende
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ javademo ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\20170801\GitProject\javademo\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ javademo ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform depende
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ javademo ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\20170801\GitProject\javademo\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ javademo ---
[INFO] Surefire report directory: C:\20170801\GitProject\javademo\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.sjmz.javademo.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13.012 s
[INFO] Finished at: 2019-01-02T23:04:38+08:00
[INFO] Final Memory: 13M/123M
[INFO] ------------------------------------------------------------------------

C:\20170801\GitProject\javademo>

测试成功,在target目录下,会生成一个test-classes目录,存放的是测试代码的class文件

构建Maven项目并上传到码云gitee【一篇就够】_第5张图片

3、打包

运行命令mvn clean package

C:\20170801\GitProject\javademo>mvn clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building javademo 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ javademo ---
[INFO] Deleting C:\20170801\GitProject\javademo\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ javademo ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ javademo ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\20170801\GitProject\javademo\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ javademo ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ javademo ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\20170801\GitProject\javademo\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ javademo ---
[INFO] Surefire report directory: C:\20170801\GitProject\javademo\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.sjmz.javademo.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ javademo ---
[INFO] Building jar: C:\20170801\GitProject\javademo\target\javademo-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.018 s
[INFO] Finished at: 2019-01-02T23:11:51+08:00
[INFO] Final Memory: 17M/208M
[INFO] ------------------------------------------------------------------------

C:\20170801\GitProject\javademo>

执行成功后,会在target目录下生成javademo-1.0-SNAPSHOT.jar包文件

构建Maven项目并上传到码云gitee【一篇就够】_第6张图片

4、安装

运行命令mvn clean install

C:\20170801\GitProject\javademo>mvn clean install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building javademo 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ javademo ---
[INFO] Deleting C:\20170801\GitProject\javademo\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ javademo ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ javademo ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\20170801\GitProject\javademo\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ javademo ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ javademo ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 1 source file to C:\20170801\GitProject\javademo\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ javademo ---
[INFO] Surefire report directory: C:\20170801\GitProject\javademo\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.sjmz.javademo.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ javademo ---
[INFO] Building jar: C:\20170801\GitProject\javademo\target\javademo-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ javademo ---
[INFO] Installing C:\20170801\GitProject\javademo\target\javademo-1.0-SNAPSHOT.jar to C:\Users\Administrator\.m2\repository\com\sjmz\javademo\javademo\1.0-SNAPSHOT\javademo-1.0-SNAPSHOT.jar
[INFO] Installing C:\20170801\GitProject\javademo\pom.xml to C:\Users\Administrator\.m2\repository\com\sjmz\javademo\javademo\1.0-SNAPSHOT\javademo-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 18.497 s
[INFO] Finished at: 2019-01-02T23:15:49+08:00
[INFO] Final Memory: 15M/118M
[INFO] ------------------------------------------------------------------------

C:\20170801\GitProject\javademo>

命令执行成功,就会将项目的jar包安装到本地仓库:

C:\Users\Administrator\.m2\repository\com\sjmz\javademo\javademo\1.0-SNAPSHOT\javademo-1.0-SNAPSHOT.jar

构建Maven项目并上传到码云gitee【一篇就够】_第7张图片

5、运行jar包

运行命令java -cp target\javademo-1.0-SNAPSHOT.jar com.sjmz.javademo.App

C:\20170801\GitProject\javademo>java -cp target\javademo-1.0-SNAPSHOT.jar com.sjmz.javademo.App
Hello World!

C:\20170801\GitProject\javademo>

四、发布本地项目到Gitee

1、在Gitee中创建仓库

输入仓库名称,与本地项目名称相同

构建Maven项目并上传到码云gitee【一篇就够】_第8张图片

选择语言 - Java

添加.gitignore - Eclipse (一般我们总会有些文件无需纳入 Git 的管理,也不希望它们总出现在未跟踪文件列表。通常都是些自动生成的文件,比如maven产生的target目录,比如IntelliJ IDEA编辑器产生的.idea目录,比如日志文件,或者编译过程中创建的临时文件等。我们可以创建一个名为 .gitignore 的文件,列出要忽略的文件模式。

添加开源许可证 - Apache v2 License

构建Maven项目并上传到码云gitee【一篇就够】_第9张图片

获取项目的gitee地址:

构建Maven项目并上传到码云gitee【一篇就够】_第10张图片

2、【前提-已安装git客户端】进入本地项目javademo根目录,在空白处,点击右键,选择“Git Bash here”

运行git clone https://gitee.com/sjmz30071360/javademo.git命令

Administrator@SJMZ MINGW64 /c/20170801/GitProject/javademo
$ git clone https://gitee.com/sjmz30071360/javademo.git
Cloning into 'javademo'...
remote: Enumerating objects: 8, done.
remote: Counting objects: 100% (8/8), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 8 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (8/8), done.

Administrator@SJMZ MINGW64 /c/20170801/GitProject/javademo
$

3、剪切javademo目录中生成的javademo文件夹,进入上一层目录(GitProject)“粘贴”,将两个javademo文件夹内容合并

4、上传本地项目内容到gitee

在javademo目录下依次运行:

git status (查看下将会上传的内容)

git add .  (. 上传所有内容)

git commit -m "init javademo project"

git push origin master

Administrator@SJMZ MINGW64 /c/20170801/GitProject/javademo
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add ..." to include in what will be committed)

        pom.xml
        src/

nothing added to commit but untracked files present (use "git add" to track)

Administrator@SJMZ MINGW64 /c/20170801/GitProject/javademo (master)
$ git add .

Administrator@SJMZ MINGW64 /c/20170801/GitProject/javademo (master)
$ git commit -m "init javademo project"
[master ea4bc07] init javademo project
 3 files changed, 69 insertions(+)
 create mode 100644 pom.xml
 create mode 100644 src/main/java/com/sjmz/javademo/App.java
 create mode 100644 src/test/java/com/sjmz/javademo/AppTest.java

Administrator@SJMZ MINGW64 /c/20170801/GitProject/javademo (master)
$ git push origin master
Enumerating objects: 17, done.
Counting objects: 100% (17/17), done.
Delta compression using up to 2 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (16/16), 1.59 KiB | 47.00 KiB/s, done.
Total 16 (delta 0), reused 0 (delta 0)
remote: Powered By Gitee.com
To https://gitee.com/sjmz30071360/javademo.git
   855fd2e..ea4bc07  master -> master

Administrator@SJMZ MINGW64 /c/20170801/GitProject/javademo (master)
$

5、确认gitee上传内容

构建Maven项目并上传到码云gitee【一篇就够】_第11张图片

 

完!!!

你可能感兴趣的:(Maven,gitee)