Skip to content

alecigne/packt-java-projects

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Java Projects

Description

My repo and notes for the Java Projects book.

Notes

Chapter 2

Ant

See the antHelloWorld project in my source code.

  • In Eclipse, the project can be imported with Import > Projects from Folder or Archive.
  • Then Run As > Ant Build will build the project as specified by the build.xml file.
  • java -jar build/jar/HelloWorld.jar is not enough to execute the project :
    no main manifest attribute, in build/jar/HelloWorld.jar
        

    We have to modify the default manifest to add an entry point, like Eclipse would propose if we used the Export > Jar file functionality.

    For Ant, this will do :

    <target name="jar" depends="dirs,compile">
      <jar destfile="${jarDir}/HelloWorld.jar" basedir="${classesDir}">
        <manifest>
          <attribute name="Main-Class" value="HelloWorld"/>
        </manifest>
      </jar>
    </target>
        
  • Let’s enhance the build.xml file from the book: let’s create a run and a clean target, and make directories only in association to a task.

Maven

mvn archetype:generate to generate a project; good alternative to Eclipse-assisted project creation.

With Java 11, add this code to use Maven:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.8.0</version>
      <configuration>
        <source>1.11</source>
        <target>1.11</target>
        <release>11</release>
      </configuration>
    </plugin>
  </plugins>
</build>

Java

Variables

In Java 11, with local variable type inference, you can type (for a local variable, as the name suggests):

var n = names.length;

Initializer block

Initializer blocks don’t need to be static. An initializer block is executed when the instance is created. More accurately, the code is compiled into the constructor and is executed with the constructor.

Nested classes

Terminology:

  • Nested classes are divided into two categories: static and non-static. A non-static nested class is also called an inner class. These are class declarations.
  • Local classes are inside a method, a constructor or an init block. These are class declarations.
  • Anonymous classes are a short form of a nested/inner/local class. These are expressions.
  • To be continued…

Chapter 3

Comparable interface and SRP

A class implementing the Comparable interface might violate the SRP. This is why Collections.sort also provides a sort method that takes two arguments: the list to be sorted and an object implementing the Comparator interface.

Set up a Maven multimodule project

Here I will recreate the structure of the genericsort multimodule Maven project from the book (p112-115).

Let’s start with a SO answer.

This will generate the parent project:

mvn archetype:generate -B \
    -DarchetypeGroupId=org.codehaus.mojo.archetypes \
    -DarchetypeArtifactId=pom-root \
    -DarchetypeVersion=RELEASE \
    -DgroupId=net.lecigne.javaprojects \
    -DartifactId=SortChapter03

We want these modules in the parent POM:

<modules>
  <module>SortSupportClasses</module>
  <module>SortInterface</module>
  <module>bubble</module>
  <module>quick</module>
  <module>Main</module>
</modules>

The modules will be added using this command:

declare -a mavenModules=("SortSupportClasses" "SortInterface" "bubble" "quick" "Main")

for mavenModule in ${mavenModules}; do 
    mvn archetype:generate -B \
        -DarchetypeGroupId=org.apache.maven.archetypes \
        -DarchetypeArtifactId=maven-archetype-quickstart \
        -DarchetypeVersion=RELEASE \
        -DgroupId=net.lecigne.javaprojects \
        -DartifactId=${mavenModule}
done

Note that Maven updates the parent POM (in my case, it also adds useless newlines).

Importing the parent project with Eclipse will also import the subprojects and create a working set.

Unit test

Unit tests are not used to find bugs.

A test case is readable if you look at it and in 15 seconds you can tell what it does.

Reimplementing everything in a single module

In SimpleSortChapter03 I implement the code from Chapter03 without the confusing and useless multimodule structure.

TODOs

  • [ ] [ch02] Study comments about Java syntax
  • [ ] [ch03] Study comments about algorithms

About

My repo for the Java Projects book by Peter Verhas

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages