Skip to content

1 Simple Tutorial

arne edited this page May 15, 2020 · 1 revision

This Tutorial describes the development of a simple contacts application. It does not represent any best practices on how to write such application. It is just a simple sample to provide an entry point to JPA Security. The code can be found in the module jpasecurity-simple-sample. This tutorial assumes basic knowledge of the Java Persistence API. Maven 2 is used to build this sample so you should have installed Maven 2 before you start.

We want to use Maven 2 to build and run our application. Maven will try to download the needed dependencies from a remote repository. So please ensure you are connected to the internet. We set up our project with the following command:

mvn -B archetype:generate \
  -DarchetypeGroupId=org.apache.maven.archetypes \
  -DgroupId=org.jpasecurity \
  -DartifactId=jpasecurity-simple-sample

This created a directory called jpasecurity-simple-sample with a file named pom.xml and a subdirectory named src in it. Within the src-directory there is a directory-structure special to Maven. We will place our classes in src/main/java. Maven already created the packages org.jpasecurity for us. Within this package Maven created a simple Hello-World-example called App.java. Maven also created src/test/java, but we don't need this directory nor its subdirectories, so we can savely delete it.

Our sample shall be compiled and executed with Java 7 and shall be packed into an executable jar file. In order to configure Maven to do so, we have to add the following configuration to the pom.xml before the <dependencies> section:

<build>
  <plugins>
    <plugin>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <source>1.7</source>
        <target>1.7</target>
      </configuration>
    </plugin>
    <plugin>
      <artifactId>maven-jar-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <addClasspath>true</addClasspath>
            <classpathPrefix>lib</classpathPrefix>
            <mainClass>org.jpasecurity.App</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
    <plugin>
      <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <outputDirectory>${project.build.directory}/lib</outputDirectory>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
</build>

You can now compile our project by issuing the following Maven command from within the jpasecurity-simple-sample folder: mvn package

If everything is configured correctly this command should end with something like "BUILD SUCCESSFUL" and you can start our application by issueing the following command:

cd target 
java -jar jpasecurity-simple-sample-1.0-SNAPSHOT.jar

If you now see the output "Hello World!" you have successfully set up this sample.

Clone this wiki locally