Build a Maven project and upload it to the code cloud gitee [one piece is enough]

One, build a Java project

create is deprecated in maven 3.0.5 and beyond, create is deprecated in maven 3.0.5 and beyond, use generate to generate the project

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>

Displaying BUILD SUCCESS means that the project is successfully built! ! !

The directory structure of the built java project is as follows:

Under the development directory src/main/java/com/sjmz/javademo, a default App class file is created with the following content:

package com.sjmz.javademo;

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

Under the test directory src/test/java/com/sjmz/javademo, a default AppTest class file is created with the following content:

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 );
    }
}

In actual project development, we usually have configuration files, such as log4j.properties, so we also need to manually create src/main/resources (storage configuration files used in project development) and src/test/resources (storage test Configuration file), as follows:

2. Description of the pom file in the project

Projects built through Maven will have a pom.xml file in the project root directory, which is the core of Maven.

1) pom means project object model
2) pom.xml contains project construction information, including project information, project dependencies, etc.
3) pom.xml file can be inherited, in large projects, sub-module pom.xml Generally inherited from the pom.xml of the parent module

The content of pom.xml is as follows:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sjmz.javademo</groupId>
  <artifactId>javademo</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>javademo</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

Description of the node elements of the pom.xml file:
    <project> The top-level node of the pom file
    <modelVersion> object model version, for Maven2 and Maven3, it can only be 4.0.0 
    <groupId>     the identifier of the project creation organization, generally the domain name The reversed writing of
    <artifactId>     defines the unique identifier of the project under the identifier of the organization to which it belongs. There can be multiple projects under one organization.
    <version> The version of the     current project, SNAPSHOT, means a snapshot version, which is
    packaged in <packaging> under development There are jar, war, ear, etc.
    <name> project name
    <url> project address
    <properties> property configuration, such as: <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <dependencies > Build the jar that the project depends on

Among them, a project coordinate is uniquely determined by groupId, artifactId and version.

Three, compile-test-package-install-run

1. Compile

Enter the javademo directory and run 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>

Compilation will create an additional target directory, and the compiled class files are stored in the classes folder.

2. Test

Run 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>

The test is successful. In the target directory, a test-classes directory will be generated, which stores the class files of the test code.

3. Packing

Run the command 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>

After the execution is successful, the javademo-1.0-SNAPSHOT.jar package file will be generated in the target directory

4. Installation

Run the command 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>

If the command is executed successfully, the jar package of the project will be installed in the local warehouse:

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

5. Run the jar package

Run the command 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>

Fourth, publish local projects to Gitee

1. Create a warehouse in Gitee

Enter the warehouse name, which is the same as the local project name

Select language-Java

Add .gitignore-Eclipse ( Generally we always have some files that do not need to be managed by Git, and we don't want them to always appear in the untracked file list. Usually they are automatically generated files, such as the target directory generated by maven, such as IntelliJ IDEA editor Idea directory generated by the compiler, such as log files, or temporary files created during compilation. We can create a file named .gitignore to list the file patterns to be ignored. )

Add open source license-Apache v2 License

Get the gitee address of the project:

2. [Prerequisite-git client installed] Enter the root directory of the local project javademo, right-click in the blank space, and select "Git Bash here"

Run the git clone https://gitee.com/sjmz30071360/javademo.git command

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. Cut the javademo folder generated in the javademo directory, enter the upper level directory (GitProject) and "paste", merge the contents of the two javademo folders

4. Upload the content of the local project to gitee

Run in sequence in the javademo directory:

git status (check what will be uploaded)

git add. (. Upload all content)

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 <file>..." 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. Confirm gitee upload content

 

Finish! ! !

Guess you like

Origin blog.csdn.net/sjmz30071360/article/details/85645049