Maven common instructions and common errors (Linux)

1. New Linux project

mvn archetype:generate, and then enter the groupId, artifactId, and version of the project you want to build at the prompt on the command line

2. Common commands

  • mvn install (installation): Install the package to the local warehouse so that the package can be used as a dependency of other local projects.
  • mvn package (package): After the source code is compiled, use a suitable format (such as JAR format) to package the compiled source code.
  • mvn clean: clean the project
  • mvn compile: compile the project source code
  • mvn test-compile: compile project test source code
  • mvn test (test): Use a suitable unit test framework to test the compiled source code.
  • java -jar *.jar
  • java -cp *.jar *(class)

The difference between install and package: Project A depends on project B. If project B only executes clean and package, it is just packaged under the target of project B. When project A is compiled again, a compilation error will be reported because project B did not execute install. So the package is only packaged under the target, and install is packaged and installed to the local maven warehouse. For packages without dependencies, there is not much difference between the two.

Reference: The difference between maven package and install

3. Practical examples

mvn archetype:generate

The node shown in the figure below will stop, ask to select the type of project (Choose archetype), and press Enter directly (default is 7: internal -> org.apache.maven.archetypes:maven-archetype-quickstart (An archetype which contains a sample Maven project.))

Insert picture description here

groupId: company name

artifactId: project name

version: version

package: package name

Insert picture description here

After the new project is completed, you can find that artifactId is the name of the project folder, and package is the name of the folder under Java.

Insert picture description here

main/java/com/test/App.java is the main class, the content is as follows:

Insert picture description here

mvn compile compiles the current project, you can find that there is more target part

Insert picture description here

mvn install is compiled into a jar package, you can find that there is an additional jar package under the target

Insert picture description here

java -jar target/com.test-1.jar, execute the jar package, report no main class error

Insert picture description here

java -cp target/com.test-1.jar com.test.App, run the corresponding class directly, and run successfully.

Insert picture description here

4. Summary of Errors

4.1 Cannot load the main class

Analysis: Lack of start-up classes

Solution: add the following content in pom.xml

<build>
    <plugins>
        <!--添加maven插件-->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>

            <!--添加自己的启动类路径!-->
            <configuration>
                <mainClass>com.test.App</mainClass>
            </configuration>

            <executions>
                <execution>
                    <goals>
                        <!--可以把依赖的包都打包到生成的Jar包中-->
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

4.2 JNI error occurs

The jdk and jre versions are the same, and this error occurs when connecting to Neo4j.

Insert picture description here

Cause: Early dawn

Solution: The main class cannot be loaded at the same time

Guess you like

Origin blog.csdn.net/MaoziYa/article/details/114270638