My name is Vasyl Khrystiuk‎ > ‎process‎ > ‎main‎ > ‎

maven simple

Create empty simple project

>mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=ua.org.msangel.j2se -DartifactId=SimpleApp -Dversion=1.0-SNAPSHOT -Dpackage=ua.org.msangel.j2se  -DinteractiveMode=false

Add dependency to pom.xml

    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20090211</version>
    </dependency>
  </dependencies>
</project>

Edit source

public class App 
{
    public static void mainString[args )
    {
        System.out.println"Hello World!" );
        String json "{\"a\":\"b\"}";
        try {
            JSONObject objj new JSONObject(json);
            System.out.printlnobjj.get("a");
            
        catch (JSONException ex{
            ex.printStackTrace();
        }
        
    }
}

 Make it runnable jar and pack all dependencies inside.

Add this section to pom.xml:
  <build>
    <plugins>
      <plugin>
        <!-- NOTE: We don't need a groupId specification because the group is
          org.apache.maven.plugins ...which is assumed by default.
        -->
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <mainClass>ua.org.msangel.j2se.App</mainClass>
            </manifest>
          </archive>
          <finalName>running</finalName<!-- optional-->
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id><!-- this is used for inheritance merges -->
            <phase>package</phase><!-- append to the packaging phase. -->
            <goals>
              <goal>attached</goal><!-- goals == mojos -->
            </goals>
          </execution>
        </executions>
        </plugin>
    </plugins
    <finalName>${project.artifactId}</finalName<!-- optional --> 
  </build>

Note: http://maven.apache.org/plugins/maven-shade-plugin/ is more applicable for this task (creating uber-jar)
For building uber-jar usually required assembly descriptors. In this case we use descriptor references to one of standart, or create own.

 Compile

>mvn clean install

 Run

 >cd target
target>java -jar SimpleApp-1.0-SNAPSHOT-jar-with-dependencies.jar
Hello World!
b

target>



Add local libraries as dependencies



http://mvnrepository.com/ - repository search


See also:

to be processed:





Other stuff

How to create simple maven project:
    *) mvn archetype:create -DgroupId=ua.org.msangel.j2se -DartifactId=SimpleApp 
deprecated

    *) mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart
with manual entering groupId, artifactId, version, package. 
Or:
    mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DgroupId=ua.org.msangel.j2se -DartifactId=SimpleApp -Dversion=1.0-SNAPSHOT -Dpackage=ua.org.msangel.j2se  -DinteractiveMode=false
Other examplers:  http://www.sonatype.com/books/mvnref-book/reference/archetype-sect-using.html
Other options:
maven-archetype-archetype
maven-archetype-j2ee-simple
maven-archetype-mojo
maven-archetype-portlet
maven-archetype-profiles
maven-archetype-quickstart
maven-archetype-simple
maven-archetype-site
maven-archetype-site-simple
maven-archetype-webapp


But really it is useless for beginners - to have only jar and nothing to do with it.
Another option: 

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.2</version>
    <!-- nothing here -->
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2-beta-4</version>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
      <archive>
        <manifest>
          <mainClass>org.sample.App</mainClass>
        </manifest>
      </archive>
    </configuration>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>


Or even:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <archive>
      <manifest>
        <mainClass>ort.path.to.MainClass</mainClass>
      </manifest>
    </archive>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
  </configuration>
</plugin>


Skipping tests:
-Dmaven.test.skip=true
or
-DskipTests=true

bilding offline:
mvn -o package

open qw:
what is maven? http://maven.apache.org/what-is-maven.html
what is POM? http://maven.apache.org/guides/introduction/introduction-to-the-pom.html
pom structure: http://maven.apache.org/pom.html
what is mirrors and repositories? http://maven.apache.org/guides/introduction/introduction-to-repositories.html http://maven.apache.org/guides/mini/guide-mirror-settings.html
what is dependency? http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

very long learning: http://www.ibm.com/developerworks/ru/edu/j-mavenv2/index.html
long learning: http://maven.apache.org/guides/getting-started/index.html
fast learning: http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
runing maven: http://maven.apache.org/run-maven/index.html
eclipse maven plugin: m2e









ċ
m-site.zip
(2k)
Vasyl Khrystiuk,
15 Jul 2012, 23:20
Comments