IntelliJ IDEA Quick Start Project Notes

Russell Bateman
July 2019

Let's create a project from scratch in IntelliJ IDEA. There are a few IDE skills and Java-practices to convey.

  1. Launch IDEA and choose File → New....\

  2. Click Java in the upper left. Set your (new) project SDK to 11 (or 12—whatever you installed privately). Click Next.

  3. Click Next again. (We're not going to use a template, but do this next bit by hand.)

  4. Name the project hapi-v2. Ensure that the Project location, Content root and Module file location are all appropriately set. The appropriate place is one you choose. I put my private projects on the path, /home/russ/dev.

    Note that I'm going to assume the following path for convenience here: /home/james/dev/hapi-v2. Click Finish.

  5. You'll get a new instance of IDEA's workspace. Open the project in the Projectpane by double-clicking on its name. You'll see:
    ▼ hapi-v2
      ▶ .idea
        src
        hapi-v2.iml
    ▶ External libraries
    
  6. Inside or outside the IDE, create or add the following subdirectories to the project. I do this by hand because it's easier for me. (Option -p tells bash not to complain about missing subdirectories, but just to create them.)
    russ@nargothrond ~ $ cd ~/dev
    russ@nargothrond ~/dev/hapi-v2 $ mkdir -p src/main/java/ca/uhn/hl7v2/examples
    russ@nargothrond ~/dev/hapi-v2 $ mkdir -p src/test/java/ca/uhn/hl7v2/examples
    
  7. Importantly, this will give you the following structure and we'll be ready to add sample code. (You should add Linux command line tree using the first command here. tree is super useful.)
    russ@nargothrond ~/dev/hapi-v2/src $ sudo apt-get install tree
    russ@nargothrond ~/dev/hapi-v2/src $ tree
    .
    ├── main
    │   └── java
    │       └── ca
    │           └── uhn
    │               └── hl7v2
    │                   └── examples
    └── test
        └── java
            └── ca
                └── uhn
                    └── hl7v2
                        └── examples
    
    

    This is important because the canonical filesystem representation of Java source code is always the following.

    russ@nargothrond ~/dev/hapi-v2/src $ tree -L 2
    .
    ├── main
    │   ├── java      production code
    │   └── resources production resources  files like log4j.properties and other data used at runtime
    └── test
        ├── java      test code
        └── resources test resources        any file data consumed by JUnit tests
    

    Note that, below this point, Java packages make up subdirectories in a hierarchy.

  8. Now click on the Project Structure icon in the toolbar. This is the same thing as doing File → Project Structure....
    1. Set the Project SDK.
    2. Set the Project language level.
    3. Set the Project compiler output (to something, anything).
    4. Click Modules at the left.
    5. Click the blue src folder, then click Sources to turn the blue off.
    6. Click .idea, then click Excluded.
    7. Open both main and test.
    8. Click java under main, then click Sources.
    9. Click java under test, then click Tests.
    10. Click resources under main, then click Resources.
    11. Click resources under test, then click Test Resources.
  9. Next, create some sample code in the project. Add ExampleParseMessage (see notes for how to get this in IntelliJ IDEA and Maven Notes) to your project. Just open the path in the Project pane down to the package, right-click the package, then choose New → Java Class, then copy and paste the code here into the editor window. Note: the result will drip with red errors. That's okay.

  10. Then, add pom.xml (below) to your project. Just create pom.xml as a new file at the root of the project (right-click and choose New → File, etc.). To get IDEA to use Maven for its build, go to the right edge of the IDE, click on Maven, then load the new POM file by clicking on the rotating arrows at the top of the Maven pane that appears. Click again on Maven at the edge of the IDE to make that pane go away.

  11. After the above, we'll work together to make the red go away. This is where I refer you to IntelliJ IDEA and Maven Notes.

Resources

Here's a basic pom.xml. It goes at the root of the project.

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>ca.uhn.hl7v2.examples</groupId>
  <artifactId>hl7-v2</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <name>hl7-v2</name>
  <description>Fun with HL7 v2</description>

  <properties>
    <junit.version>4.12</junit.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <build>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.3</version>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
<!-- vim: set tabstop=2 shiftwidth=2 expandtab: -->

When we've finished, pom.xml will look like this.

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>ca.uhn.hapi</groupId>
  <artifactId>hapi-v2</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <name>hapi-v2</name>
  <description>Fun with HAPI</description>

  <properties>
    <hapi_v2.version>2.3</hapi_v2.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <build>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.3</version>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>ca.uhn.hapi</groupId>
      <artifactId>hapi-base</artifactId>
      <version>${hapi_v2.version}</version>
    </dependency>
    <dependency>
      <!-- ExampleParseMessages -->
      <groupId>ca.uhn.hapi</groupId>
      <artifactId>hapi-structures-v22</artifactId>
      <version>${hapi_v2.version}</version>
    </dependency>
  </dependencies>
</project>
<!-- vim: set tabstop=2 shiftwidth=2 expandtab: -->