/java-tutorial

Step-by-side Java tutorial

Primary LanguageJava

Create initial project structure using maven

  1. f you don't have maven installed (type mvn on terminal to check whether it exists), install maven. On Mac use
    brew install maven

  2. First time you run any maven command it will download lots of dependencies and print lots of download statements on console. Second time onwards, it will be faster and won't fill up your screen.

  3. Create a new Java project with maven
    mvn archetype:generate

  4. Select default options for for first two questions. Just hit enter twice.

  5. Enter artifact-id (com.shital) and group id (tutorial).

  6. Select default value for version and package.

  7. Confirm all properties.

maven will create a basic directory structure suitable for Java projects as follows:

|-pom.xml  
 |-src  
 | |-test  
 | | |-java  
 | | | |-com  
 | | | | |-shital  
 | | | | | |-AppTest.java  
 | |-main  
 | | |-java  
 | | | |-com  
 | | | | |-shital  
 | | | | | |-App.java  

Now compile and run your Main class

  1. Using compile and exec plugins of maven. You need to specify the main class in the argument as follows: mvn compile exec:java -Dexec.mainClass="com.shital.App"

  2. Put main Class in pom.xml so that you don't have to specify it at command line. Insert following inside tag in pom.xml

    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <goals>
              <goal>java</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <mainClass>com.shital.App</mainClass>
        </configuration>
      </plugin>
    </plugins> 	
  3. Now you can simply type following to run your program. No need to specify main class

    mvn compile exec:java
    
  4. Maven creates all classes inside target/classes dir. To simply run using java commmand, do following in the base dir

    cd target/classes
    java com.shital.App
    
  5. Now run all uni tests. Check dummy test written in src/test/java/com/shital/AppTest.java

    mvn test
    
  6. You should see following on your console. This means all tests passed and there are no test failures.

    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0