A Maven and IntelliJ Mash-up

Malina Tran
Tech and the City
Published in
3 min readAug 29, 2016

So, I decided to reorganize my nascent Java program.

First things first is installing Maven. I am using IntelliJ and it proved easy enough to add frameworks support to a module. This changed my project structure so that it became a directory mash-up between Maven and IntelliJ, and it looked like this (it actually goes a couple of layers deeper than this):

. java-http-server
-- java-http-server.iml
-- out
-- pom.xml
-- src
-- main
-- java
-- com
-- company
-- test
-- java
-- com
-- company
-- target
-- main
-- test

You’ll notice that an XML file called POM (for Project Object Model) was created —this contains information about the project and configuration details for building the project.

Your impulse may be to delete seemingly extraneous folders, and I just want to caution that this is possible (I recall a folder called `resources` that I removed without any ramifications), but to proceed with caution.

For instance, I deleted my `out` folder which stores the output of the JAR file built by the IDE. It has no implications; if you rebuild the JAR file, an `out` folder will be created.

I originally had all of my test files in one folder and it made sense, with the new project structure, to break code into `src > main` and unit tests to `src > test`. You may feel compelled to remove the `com > company` folder/subfolder — which is intact after the mash-up happened — but it corresponds with the package name and is considered Java convention. The `com > company` file structure should mirror each other in both `src > main` and `src > test`.

I added JUnit as a dependency in my pom.xml file:

 <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
</dependencies>

And set up the entry point of my program in the same file:

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<version>2.6</version>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.malinatran.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

At this point in time, you can either stick to IntelliJ’s GUI to accomplish certain tasks, e.g. build JAR file and run tests, or run Maven commands. According to my colleague and friend Matt Higgins:

“I mean it’s way more convenient to use IntelliJ to run a single unit test, or tests in a particular file a button is easier than a command, even if the IDE is slower to run the tests. That doesn’t matter as much when you’re talking about 1–10 tests or something like that. If you’re running the whole suite, I think `mvn test` is nicer because it’s faster and the command isn’t too long
but then again, IntelliJ gives much nicer output, especially for failing tests, than maven does.”

If you decide that you’d like to use IntelliJ for these tasks, make sure that IntelliJ recognizes all of your stored folders. Go to `File > Project Structure` and in the dialog that appears, click on `Modules`in the left-hand sidebar and the `Sources` tab. The color-coding is super nifty, and will look like the screenshot below. Make sure `Source Folders` and `Test Source Folders` correspond to your new file structure.

I’ve started relying on the command line to do things, so if you’d like to do the same, feel free to do the following:

// Run a single test file, e.g. file name is called "Request Test
mvn test -Dtest=RequestTest
// Run all the tests
mvn test
// Generate a JAR file in your `target` folder:
mvn package

Shout out to Matt for helping me out with the setup!

--

--