Using Maven

Unlike Ant, Maven helps you create the skeleton of a new project. To do that, you will have to type the following command:

    $ mvn archetype:generate

Maven will first download the actually available project types from the network and prompt you to select the one you want to use. This approach seemed to be a good idea while Maven was new. When I first started Maven, the number of listed projects was somewhere between 10 and 20. Today, as I write this book, it lists 1,635 different archetypes. This number seems more like a historical date (the constitution of the French Academy of Science) than a usable size list of different archetypes. However, do not freak out. Maven offers a default value when it asks for your choice, and it is good for the HelloWorld we go for.

    Choose a number: 817: 

The actual number may be different on your installation. Whatever it is, accept the suggestion and press Enter. After that, Maven will ask you for the version of the project:

    Choose version: 
1: 1.0-alpha-1
2: 1.0-alpha-2
3: 1.0-alpha-3
4: 1.0-alpha-4
5: 1.0
6: 1.1
Choose a number: 6: 5

Select the 1.0 version that is listed as number 5. The next thing Maven asks for is the group ID and the artifact ID of the project. The dependency management that we will discuss later uses these. I selected a group ID based on the book and the publisher. The artifact of the project is SortTutorial as we will start our chapter example in this project.

Define value for property 'groupId': : packt.java9.by.example
Define value for property 'artifactId': : SortTutorial

The next question is the current version of the project. We have already selected 1.0 and Maven offers 1.0-SNAPSHOT. Here, I selected 1.0.0-SNAPSHOT because I prefer semantic versioning.

Define value for property 'version':  1.0-SNAPSHOT: : 1.0.0-SNAPSHOT
Semantic versioning, defined on http://semver.org/, is a versioning scheme that suggests three digit version numbers as M.m.p. for Major, minor, and patch version numbers. This is very useful for libraries. You will increment the last version number if there is only a bug fix since the previous release. You will increment the minor number when the new release also contains new features, but the library is compatible with the previous version; in other words, any program that is using the older version can still use the newer version. The major release number is increased when the new version is significantly different from the previous one.
In the case of application programs, there is no code that uses the application API; thus, the minor version number is not that important. It does not hurt, though, and it often proves to be useful to signal smaller changes in the application. We will discuss how to version software in the last chapter.

Maven handles the versions that have the -SNAPSHOT postfix as non-release versions. While we develop the code, we will have many versions of our code, all having the same snapshot version number. On the other hand, non-snapshot version numbers can only be used only for a single version.

Define value for property 'package':  packt.java9.by.example: :

The last question from the program skeleton generation is the name of the Java package. The default is the value we gave for groupId, and we will use this. It is a rare exception to use something else.

When we have specified all the parameters that are needed, the final request is to confirm the setting:

Confirm properties configuration: 
groupId: packt.java9.by.example
artifactId: SortTutorial
version: 1.0.0-SNAPSHOT
package: packt.java9.by.example
Y: : Y

After entering Y, Maven will generate the files that are needed for the project and display the report about this:

[INFO] ----------------------------------------------------------- 
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] -----------------------------------------------------------
[INFO] Parameter: basedir, Value: .../mavenHelloWorld
[INFO] Parameter: package, Value: packt.java9.by.example
[INFO] Parameter: groupId, Value: packt.java9.by.example
[INFO] Parameter: artifactId, Value: SortTutorial
[INFO] Parameter: packageName, Value: packt.java9.by.example
[INFO] Parameter: version, Value: 1.0.0-SNAPSHOT
[INFO] *** End of debug info from resources from generated POM ***
[INFO] project created from Old (1.x) Archetype in dir: .../mavenHelloWorld/SortTutorial
[INFO] -----------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] -----------------------------------------------------------
[INFO] Total time: 01:27 min
[INFO] Finished at: 2016-07-24T14:22:36+02:00
[INFO] Final Memory: 11M/153M
[INFO] -----------------------------------------------------------

You can take look at the following generated directory structure:

You can also see that it generated the following three files:

  • SortTutorial/pom.xml that contains the Project Object Model
  • SortTutorial/src/main/java/packt/java9/by/example/App.java that contains a HelloWorld sample application
  • SortTutorial/src/test/java/packt/java9/by/example/AppTest.java that contains a unit test skeleton utilizing the junit4 library

We will discuss unit tests in the next chapter. For now, we will focus on the sorting application. As Maven was so kind and generated a sample class for the app, we can compile and run it without actual coding, just to see how we can build the project using Maven. Change the default directory to SortTutorial issuing cd SortTutorial and issue the following command:

    $ mvn package

We will get the following output:

Maven fires up, compiles, and packages the project automatically. If not, please read the next info box.

When you first start Maven, it downloads a lot of dependencies from the central repository. These downloads take time, and are reported on the screen, so the actual output may be different from what you saw in the preceding code.
Maven compiles code with the default settings for Java version 1.5. It means that the generated class file is compatible with Java version 1.5, and also that the compiler only accepts language constructs that were available already in Java 1.5. If we want to use newer language features, and in this book we use a lot, the pom.xml file should be edited to contain the following lines:

<build>

    <plugins>

      <plugin>

        <groupId>org.apache.maven.plugins</groupId>

        <artifactId>maven-compiler-plugin</artifactId>

        <configuration>

          <source>1.9</source>

          <target>1.9</target>

        </configuration>

      </plugin>

    </plugins>

  </build>

When using Java 9's default settings for Maven, it becomes even more complex, because Java 9 does not generate class format nor restrict source compatibility earlier than Java 1.6. At this very moment, as I write these lines, the latest Maven release is 3.3.9. When I try to compile the preceding code without the modifications, the Java compiler stops with an error displaying the following:
[ERROR] Source option 1.5 is no longer supported. Use 1.6 or later.
[ERROR] Target option 1.5 is no longer supported. Use 1.6 or later.

Later, Maven releases may behave differently in the future.

Now, you can start the code using the following command:

    $ java -cp target/SortTutorial-1.0.0-SNAPSHOT.jar packt.java9.by.example.App

You can see the result of a sample run in the following picture:


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.123.147