Installing the Archetype
We’ll start by cloning the archetype repository so we can install it. If the archetype will be published in Maven Central at some point, this step will be different but for now we will do it like this. Make sure Eclipse is open and ready.
Clone
https://github.com/diging/spring-thymeleaf-archetype.git
using your favorite Git client.Go to Eclipse.
Right-click into the Project Explorer or if that doesn’t work choose “File > Import…”. Or if you see a list of options in the Project Explorer (if you have an empty workspace, you might see this) click “Import projects…”
In the import wizard, choose “Projects from Git” from the list and click “Next.”
Choose “Existing local repository” and click “Next.”
Add the Git repository you cloned in the first step, select
spring-thymeleaf-archetype
from the list that should appear after you added the repository, and click "Next."Make sure “Import existing Eclipse project” is selected, open the “Working Tree” folder and select
archetype
. Click "Next."Make sure
archetype
is selected. Click “Finish.”The project should now show up in your Project Explorer with a little M decorator indicating that it’s a Maven project.
Right-click on the project and choose “Run as > Maven install.”
In the console, you should see a bunch of output ending with something like this
[INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 24.405 s [INFO] Finished at: 2023-01-18T12:39:37-05:00 [INFO] ------------------------------------------------------------------------
The Maven archetype (Maven template) is now installed in your local Maven repository.
Creating a new project
Now we can create a project from the archetype.
Right-click in the Project Explorer and select “New > Project…”
In the New Project wizard, select open the
Maven
folder and select “Maven Project.” Click “Next.”On the “New Maven project” page, make sure “Create a simple project (skip archetype selection)” is not selected. Click “Next.”
On the “Select an archetype” page, choose “Default Local” from the “Catalog” dropdown. The archetype we just installed, should show up.
Select the archetype (
spring-thymeleaf-webapp
) and click "Next."Enter a group id and artifact id. The values do not matter too much, but the group and artifact ids will be used as base package name by default. Click “Finish.”
Maven is now creating your project. This might take a while. In the console you can probably see Maven’s log output. If the console stops in between asking you to confirm the properties configuration, just hit enter.
Your project structure of the newly created project, should look something like this:
If you are using Java 11, you will probably see the above errors on the project. To fix those:
open
pom.xml
find the plugin with the artifact id
maven-war-plugin
Upgrade the version to 3.3.1
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>3.3.1</version> <configuration> <webResources> <resource> <filtering>true</filtering> <directory>src/main/webapp</directory> <includes> <include>**/*</include> </includes> <excludes> <exclude>**/*.woff</exclude> <exclude>**/*.ttf</exclude> <exclude>**/*.woff2</exclude> </excludes> </resource> </webResources> </configuration> </plugin>
Next, we need to fix the compilation issues in SimpleUsersConfig
.
Open
SimpleUsersConfig.java
Remove lines 4 and 5.
import my.project.simpleusers.core.config.SimpleUsers; import my.project.simpleusers.core.config.SimpleUsersConfiguration;
Organize imports by either right-clicking in the editor and selecting “Source > Organize Imports” or by using the keyboard shortcut (
cmd-shift-o
on Mac).Save the file. The compilation errors should be gone now.
Running the project
To run the project:
Right-click it, and select “Run as > Run on Server.”
In the dialog, open the “Apache” folder and select “Tomcat v9.0 Server.”
Click “Next,” select the Tomcat installation directory (make sure you have downloaded Tomcat 9.0).
Click “Finish.”
The server should be starting but there should be exceptions in the log. You should find an exception in the log that says:
Caused by: java.lang.RuntimeException: Driver com.mysql.jdbc.Driver claims to not accept jdbcUrl, jdbc-url-to-db at com.zaxxer.hikari.util.DriverDataSource.<init>(DriverDataSource.java:108) at com.zaxxer.hikari.pool.PoolBase.initializeDataSource(PoolBase.java:336) at com.zaxxer.hikari.pool.PoolBase.<init>(PoolBase.java:109) at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:108) at com.zaxxer.hikari.HikariDataSource.<init>(HikariDataSource.java:81) at my.project.thymeleaf.config.PersistenceConfig.dataSource(PersistenceConfig.java:41) at my.project.thymeleaf.config.PersistenceConfig$$EnhancerBySpringCGLIB$$f9717ade.CGLIB$dataSource$0(<generated>) at my.project.thymeleaf.config.PersistenceConfig$$EnhancerBySpringCGLIB$$f9717ade$$FastClassBySpringCGLIB$$1490bd12.invoke(<generated>) at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) at my.project.thymeleaf.config.PersistenceConfig$$EnhancerBySpringCGLIB$$f9717ade.dataSource(<generated>) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ... 70 more
You see this exception because we have not yet setup a database to which the application can connect. Currently, the application tries to connect to a database jdbc-url-to-db
, which does not exist. To fix this, create a new MySQL database in your favorite client and a user that has write access to that database (the user needs to be able to create tables, change them, etc).
If you are using the MySLQ command line client you can do this in three lines:
create database thymeleaf;
create user 'app'@'localhost' identified by 'app';
grant all on thymeleaf.* to 'app'@'localhost';
Then provide the details of the database in the POM file.
Open
pom.xml
.Find the part with the database properties.
<db.driver>com.mysql.jdbc.Driver</db.driver> <db.database.url>jdbc-url-to-db</db.database.url> <db.user>db-user</db.user> <db.password>db-password</db.password>
Replace
jdbc-url-to-db
with the url to the database (something likejdbc:mysql://localhost:3306/databaseName
)Replace
db-user
with the db user’s username.Replace
db-password
with the password of the db user.Now right-click the project and select “Maven > Update Project…”
Make sure
thymeleaf
is selected and click “Ok.”Restart the server.
The server should now start without errors and you should get the main app home page at http://localhost:8080/thymeleaf/
.