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/0http://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
新聞熱點
疑難解答