maven的hello world

软件:maven

maven的第一个程序


1 由于maven的quick start不好使用(不明原因)

所以第一个hello world采用部分手工的方法创建。

2 创建helloworld.java和pom.xml


src/main/java/hello/HelloWorld.java

package hello;

public class HelloWorld {
    public static void main(String[] args) {
        Greeter greeter = new Greeter();
        System.out.println(greeter.sayHello());
    }
}

其中Greeter.java


package hello;

public class Greeter {
    public String sayHello() {
        return "Hello world!";
    }
}

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/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>org.springframework</groupId>
    <artifactId>gs-maven</artifactId>
    <packaging>jar</packaging>
    <version>0.1.0</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>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>hello.HelloWorld</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

3 执行编译

编译成class: mvn compile

编成jar: mvn package

4 执行

cd target

java -c gs*.jar

Hello world!



参考文档:

1 http://spring.io/guides/gs/maven/

2 http://www.eclipse.org/jetty/documentation/9.0.0.M3/jetty-maven-helloworld.html

你可能感兴趣的:(maven的hello world)