Wednesday, May 11, 2016

Creating of a maven project for Play application

While developing with Play/Java maven is more preferable, then sbt. It is the "native" Java tool and has a lot of plugins. Another reason to use maven - when a Play application is a part of a project, managed with maven.

Create application


1.Create a new application my-play-app according to the post Getting started with Play Framework.
2.Create for it the eclipse project with sbt eclipse command according to the post eclipse IDE setting.
3.Add to build.sbt:
// The Typesafe repository
resolvers += "Local Maven Repository" at "file:///"+Path.userHome.absolutePath+"/.m2/repository"

Import project to eclipse


In Eclipse:
1. From the File menu select Import.
2. In the Select dialog chose the General, Existing Projects into Workspace and press Next.
3. In the Import Projects dialog browse to the project directory and press OK. You will get something like this:


4. Select the project check box and press Finish

The project my-play-app will appear in the project explorer.

Convert a java project to maven project


In eclipse:
1. Right-click on the project and select Configure, Convert to Maven Project.
2. In the Create new POM dialog fill the project mandatory attributes: group id, artifact id and version. The rest attributes are optional. The default value for packaging is jar, but I prefer to select it explicitly. Press Finish:


The generated pom.xml should be changed manully:
1. Delete lines:
   <sourceDirectory>app</sourceDirectory>  
   <testSourceDirectory>test</testSourceDirectory>  
   <resources>  
    <resource>  
     <directory>app</directory>  
     <excludes>  
      <exclude>**/*.java</exclude>  
     </excludes>  
    </resource>  
    <resource>  
     <directory>conf</directory>  
     <excludes>  
      <exclude>**/*.java</exclude>  
     </excludes>  
    </resource>  
    <resource>  
     <directory>target/scala-2.11/twirl/main</directory>  
     <excludes>  
      <exclude>**/*.java</exclude>  
     </excludes>  
    </resource>  
    <resource>  
     <directory>target/scala-2.11/routes/main</directory>  
     <excludes>  
      <exclude>**/*.java</exclude>  
     </excludes>  
    </resource>  
   </resources>  


2. Add property:
   <properties>  
        <build.script>build_app.bat</build.script>  
   </properties>  

3. Modify generated compiler plugin like this:
   <plugin>  
        <groupId>org.apache.maven.plugins</groupId>  
        <artifactId>maven-compiler-plugin</artifactId>  
        <version>3.3</version>  
        <configuration>  
             <source>1.8</source>  
             <target>1.8</target>  
        </configuration>  
   </plugin>  

4. Add execution plugin:
   <plugin>  
        <groupId>org.codehaus.mojo</groupId>  
        <artifactId>exec-maven-plugin</artifactId>  
        <version>1.4.0</version>  
        <executions>  
             <!-- Execute activator -->  
             <execution>  
                  <id>execute.sbt</id>  
                  <phase>compile</phase>  
                  <goals>  
                       <goal>exec</goal>  
                  </goals>                                
                  <configuration>  
                       <executable>${build.script}</executable>  
                       <workingDirectory>${project.basedir}</workingDirectory>             
                  </configuration>  
             </execution>  
        </executions>  
   </plugin>  

The full pom.xml:
 <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>com.codepreference.my-play-app-example</groupId>  
  <artifactId>my-play-app</artifactId>  
  <version>1.0.0-SNAPSHOT</version>  
       <properties>  
           <build.script>build_app.bat</build.script>  
      </properties>  
  <build>  
   <plugins>  
           <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-compiler-plugin</artifactId>  
                <version>3.3</version>  
                <configuration>  
                     <source>1.8</source>  
                     <target>1.8</target>  
                </configuration>  
           </plugin>  
           <plugin>  
                <groupId>org.codehaus.mojo</groupId>  
                <artifactId>exec-maven-plugin</artifactId>  
                <version>1.4.0</version>  
                <executions>  
                     <!-- Execute activator -->  
                     <execution>  
                          <id>execute.sbt</id>  
                          <phase>compile</phase>  
                          <goals>  
                               <goal>exec</goal>  
                          </goals>                                
                          <configuration>  
                               <executable>${build.script}</executable>  
                               <workingDirectory>${project.basedir}</workingDirectory>             
                          </configuration>  
                     </execution>  
                </executions>  
           </plugin>                 
   </plugins>  
  </build>  
 </project>  

5. Create a file build_app.bat in the project root directory. (This file was mentioned in the execution plugin configuration in pom..)
Put into the file the following lines:
 @ECHO ON  
 SET JAVA_HOME=%JDK_8%  
 SET SBT_COMMAND_TEST=test  
 SET SBT_COMMAND=dist  
 REM          Execute the SBT tests. (temporary commented out)  
 ::     echo Execute the SBT tests.  
 ::     CALL sbt.bat -Dsbt.repository.config=repositories_windows.txt %SBT_COMMAND_TEST%  
 ::     if %ERRORLEVEL% NEQ 0 GOTO BAD_EXECUTION  
 ::     echo OK.  
 ::     echo.  
 REM          Execute the SBT.  
 echo Execute the SBT.  
 CALL sbt.bat -Dsbt.repository.config=repositories_windows.txt %SBT_COMMAND%  
 if %ERRORLEVEL% NEQ 0 GOTO BAD_EXECUTION  
 echo OK.  
 echo.  
 REM          Exit OK.  
 exit /b 0  
 :BAD_EXECUTION  
 echo.  
 echo Error while executing the activator.  
 echo.  
 exit /b 2  

Now the project may be compiled with either the maven or with the sbt.

How to define mvn clean install run configuration in eclipse


Eclipse provides several default maven configurations, like clean, build, test etc. But there is no the favorite mvn clean install configuration among the default ones. Let's create it.

In eclipse:
1.From the Run menu select Run Configurations....
2.In the Run Configurations dialog select Maven Build and press New.
3.Enter the new configuration values as shown on the picture:

Now the project may be "cleaned-installed" by selecting of the run configuration from the Run Configurations... dialog or right from the Run button the toolbar:

All the sources on Git.

No comments :

About the author

My Photo
I trust only simple code and believe that code should be handsome. This is not a matter of technology, but professional approach, consolidated after years of software development. I enjoy to cause things working and feel very happy, when I manage to solve a problem.
Back to Top