maven第一坑

来源:互联网 发布:js修改placeholder值 编辑:程序博客网 时间:2024/06/18 14:01

maven 命令行指令

mvn clean complie / test / package/ install

cd target

java -jar xxx.jar

刚啃完linux,开始啃maven。按照《maven实战》的教程,使用命令行进行打包。遇到了一些问题。

首先,最坑爹的一点是,书上并没有按照maven官方建议的目录结构进行建立。可能由于还没有讲到目录结构的原因。

但是按照书中建立目录结构,导致一直运行不成功。

从mvn clean test命令开始,就有问题,如果直接打包运行,遇到的错误为:找不到主类。

maven的目录结构:

src

  |     main

  |           |          java            ----------com.test.helloworld.HelloWorld.java

  |           |          resources

  |           |          filters

  |       test

  |           |        java             ----------HelloWorldTest.java

  |           |        resource

  |           |        filters

  |       site

  |       assembly

 target

 pom.xml


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/XNLSchema-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.test</groupId>
  <artifactId>hello-world</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>Maven Hello World Project</name>
  <dependencies>
    <dependency>
        <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        <version>4.7</version>
        <scope>test</scope>
    </dependency>
  </dependencies>
  <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-shade-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                            <mainClass>main.java.com.test.helloworld.HelloWorld</mainClass> <!-- 指明主类-->
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>

  </build>


</project>

HelloWorldTest

package test.java;

import main.java.com.test.helloworld.HelloWorld;
import junit.framework.Assert;
import junit.framework.TestCase;
//导入的包名和书中不一致,应该是由于版本的历史原因?
public class HelloWorldTest extends TestCase{

    //@Test        注解被注释掉了,不知道为什么会出错。
    public void testSayHello(){
        HelloWorld helloWorld = new HelloWorld();
        String result = helloWorld.sayHello();
        assertEquals("Hello Maven", result);
    }
}


HelloWorld:


package main.java.com.test.helloworld;

public class HelloWorld{

        public String sayHello(){
                return "Hello Maven";
           }

        public static void main(String[] args){
                System.out.print(new HelloWorld().sayHello());
        }

}


不在IDE里,打印错误是常见错误。QAQ

0 0
原创粉丝点击