Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Desktop Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « SmalltalkKotlin and testing »
    comments

    Kotlin and Maven

    by Christiaan Baes (chrissie1) on Dec 03, 2012 in categories Java

    Introduction

    Last week I asked if Spek was on a Maven repository so I could easily add it to my project and test it.
    Someone who will remain anonymous (who still needs to wash my car) and runs the Spek project told me it wasn't and that I could be so nice to add it.

    I know next to nothing about Maven, except that it exists. So I went on the Google and found... a lot of things that I didn't know about. But in the end I found this.

    And that helped just a little.

    Setting up Maven

    Apparently everybody knows how to set up Maven but forget to tell how it is done.
    So first of all I downloaded Maven 3.0.4 and then unzipped that in the Program files folder (x86). Then I had to add this E:\Program Files (x86)\apache-maven-3.0.4\bin to my Path in the System Variables whiyou can find in Control Panel -> System -> Advanced settings -> Environment variables. I also needed to add the system variable JAVA_HOME and set it to this E:\Program Files (x86)\Java\jdk1.7.0\jre or the path to your jre of your JDK.

    You can also add the system variable M2_HOME and set it to this E:\Program Files (x86)\apache-maven-3.0.4 or not. You can choose to add it manually to your module too.

    If you get this error

    Error running kotlindeom [test]: No valid Maven installation found. Either set the home directory in the configuration dialog or set the M2_HOME environment variable on your system.

    then the above did not work. In Intellij go to File -> Settings -> Maven and copy the Maven directory in the Maven home directory field.

    And now Maven works with Intellij 12. Oh joy.

    MavenModule

    It is now time to make our project.

    Make a new project and make it a Maven module.

    use the quickstart archetype, or any other archetype since we are not going to use it anyway.

    override the maven home directory if you did not set M2_HOME or if you so desire.

    Now we can change the pom.xml file to this.

    1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 <a href="http://maven.apache.org/xsd/maven-4.0.0.xsd">">http://maven.apache.org/xsd/maven-4.0.0.xsd"></a>
    3.  
    4.     <modelVersion>4.0.0</modelVersion>
    5.  
    6.     <groupId>kotlindemos</groupId>
    7.     <artifactId>kotlindeom</artifactId>
    8.     <version>1.0-SNAPSHOT</version>
    9.  
    10.     <properties>
    11.         <kotlin.version>0.1-SNAPSHOT</kotlin.version>
    12.         <junit.version>4.11</junit.version>
    13.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    14.     </properties>
    15.  
    16.     <dependencies>
    17.         <dependency>
    18.             <groupId>org.jetbrains.kotlin</groupId>
    19.             <artifactId>kotlin-stdlib</artifactId>
    20.             <version>${kotlin.version}</version>
    21.         </dependency>
    22.  
    23.         <dependency>
    24.             <groupId>junit</groupId>
    25.             <artifactId>junit</artifactId>
    26.             <version>${junit.version}</version>
    27.             <scope>test</scope>
    28.         </dependency>
    29.     </dependencies>
    30.  
    31.     <build>
    32.         <sourceDirectory>${project.basedir}/src/main</sourceDirectory>
    33.         <testSourceDirectory>${project.basedir}/src/test</testSourceDirectory>
    34.  
    35.         <plugins>
    36.             <plugin>
    37.                 <artifactId>kotlin-maven-plugin</artifactId>
    38.                 <groupId>org.jetbrains.kotlin</groupId>
    39.                 <version>${kotlin.version}</version>
    40.  
    41.                 <configuration/>
    42.                 <executions>
    43.                     <execution>
    44.                         <id>compile</id>
    45.                         <phase>compile</phase>
    46.                         <goals>
    47.                             <goal>compile</goal>
    48.                         </goals>
    49.                     </execution>
    50.                     <execution>
    51.                         <id>test-compile</id>
    52.                         <phase>test-compile</phase>
    53.                         <goals>
    54.                             <goal>test-compile</goal>
    55.                         </goals>
    56.                     </execution>
    57.                 </executions>
    58.             </plugin>
    59.         </plugins>
    60.     </build>
    61.  
    62.     <repositories>
    63.         <repository>
    64.             <id>jetbrains-all</id>
    65.             <url>http://repository.jetbrains.com/all</url>
    66.         </repository>
    67.     </repositories>
    68.  
    69.     <pluginRepositories>
    70.         <pluginRepository>
    71.             <id>jetbrains-all</id>
    72.             <url>http://repository.jetbrains.com/all</url>
    73.         </pluginRepository>
    74.     </pluginRepositories>
    75. </project>

    If everything is well you should not see any red in there.

    Does it work?

    Now we can start adding our kotlin files in.

    Create a package kotlin.kotlindemo in the main and remove the package java. Add a kotlin file to that and use this code.

    1. package demo
    2.  
    3. data class MyClass(val i : Int, val s : String)

    Now add a kotlin.kotlindemo package to the test folder and add a kotlin file.

    With this code.

    1. package demo.test
    2.  
    3. import demo.MyClass
    4. import kotlin.test.assertEquals
    5. import org.junit.Test as test
    6. import kotlin.test.assertTrue
    7. import kotlin.test.assertFalse
    8.  
    9. public class TestMyClass
    10. {
    11.     test fun IfPropertyIIsSetCorrectly()
    12.     {
    13.         val c1 = MyClass(1,"t")
    14.         assertEquals(1,c1.i)
    15.     }
    16.  
    17.     test fun IfPropertySIsSetCorrectly()
    18.     {
    19.         val c1 = MyClass(1,"t")
    20.         assertEquals("t",c1.s)
    21.     }
    22.  
    23.     test fun If2MyClassesWithTheSamePropertiesAreEqual()
    24.     {
    25.         val c1 = MyClass(1,"t")
    26.         val c2 = MyClass(1,"t")
    27.         assertTrue(c1.equals(c2))
    28.     }
    29.  
    30.     test fun If2MyClassesWithDifferentPropertiesAreNotEqual()
    31.     {
    32.         val c1 = MyClass(1,"t")
    33.         val c2 = MyClass(2,"t")
    34.         assertFalse(c1.equals(c2))
    35.     }
    36.  
    37.     test fun IfToStringReturnsTheCorrectString()
    38.     {
    39.         val c1 = MyClass(1, "t")
    40.         assertEquals("MyClass(i=1, s=t)",c1.toString())
    41.     }
    42. }

    You might think this is the same class as in my previous blogpost but it isn't. My previous class was called testMyClass and by convention Maven only finds Test*, *Test or TestCase* classes. I found that out the hard way. Lucky me.

    Anyway, your project should look like this.

    And now when we open our Maven project view. we should see this.

    you can now doubleclick on test and see the magic happen.

    This should be the result.

    5 tests run and none faild. Woohoo.

    Conclusion

    It took me a while to get it to work since I could not find Maven and IntelliJ for dummies and the Maven site is full of XML. But it works and I'm happy. Now on to Spek and getting the package into a repo, be afraid, be very afraid.

    About the Author

    User bio imageChris is awesome.
    Social SitingsTwitterHomePageLTD RSS Feed
    kotlin, maven
    InstapaperVote on HN

    No feedback yet

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)