SYS-CON MEDIA Authors: Liz McMillan, Carmen Gonzalez, Elizabeth White, Pat Romanski, Gary Arora

Blog Feed Post

Creating a Maven Archetype from a #KFCStandard JavaFX Project

In this article I will discuss the creation of a Maven Archetype that my students or anyone else can use to begin developing a JavaFX application. Like so many adventures in programming this process began quickly and then bogged down in dealing with specific details. If you want to create your own archetype then this article will be helpful. Don’t forget to read the Sip of Whine section at the end.

The source code for the sample JavaFX project is at https://github.com/omnijava/SimpleJavaFX

The archetype folder ready for installation on your system is in the release of https://github.com/omnijava/ArchetypeJavaFX/releases

Step 1: Write a program

Archetypes can be created using either a bottom up or top down approach. By far the easiest approach is top down. You begin by writing a very simple Maven managed JavaFX application. You are providing a skeleton of an application that contains required or recommended packages, resources and source code.

The application I created is a simple JavaFX Stage with one Scene with a button that when pressed display some text. The code that delivers this experience is in the format that I want my students to follow. A few years ago some students created a hash tag to poke fun at my stringent rules, #KFCStandard. That I how I describe any rules or instructions that I provide to students that must be followed. This project exemplifies a number of #KFCStandards in the areas of pom files, how to start a JavaFX application, how to do unit testing and how to comment code. If you are not one of my students then you may have issues with my archetype. I would really like to hear your thoughts. Living in academia I worry that I loose site of the real world of software development.

Do your best to ensure that the program works. If you are creating an archetype where the files are empty waiting for the user to fill them in then this is likely not going to be a problem. If your code does something, as mine does, spend some time with it to get it right.

Archetype Tip #1: Always create packages that begin with the maven groupId and optionally the artifactId. This allows the user of your archetype to use their own package names.

Archetype Tip #2: If you reference the mainClass in the pom.xml as:

        <mainClass>com.kfcstandard.javafxarchetype.MainAppFX</mainClass>

Change it to

        <mainClass>${groupId}.${artifactId}.MainAppFX</mainClass>

Archetype Tip #3: If you need to have a package created when the archetype is used but you don’t have anything to place in the package then add an empty file. Maven will not create packages if there are no files in it.

pom.xml note: When I compile with NetBeans I get two warnings regarding the pom.xml. One tells me that ${groupId} is deprecated in favour of ${project.groupId} and the second tells me that ${artifactId} is deprecated in favour of ${project.artifactId}. The NetBeans 8.01 editor only recognizes ${project.groupId} and not the other. Therefore for now I am using the deprecated versions.

Step 2: Install Maven

I use NetBeans, my personal preference for an IDE and what I used in developing the archetype code. In one course that I teach I must use Eclipse. Both of these IDEs and others now provide good support for Maven. People who know me will now roll their eyes as I say that creating an archetype is easier to do at the command line. Download and install Maven from https://maven.apache.org/download.cgi. Read the install notes at https://maven.apache.org/install.html. You need to have Maven’s bin folder in the executable path and Windows users must have a JAVA_HOME environment variable.

Maven note: Some IDEs, such as NetBeans, contain the maven command line program. I recommend downloading Maven so that you are working with the most recent version.

Step 3: Clean up the project

Locate the folder your project is in. Delete everything in the project’s folder except for the src folder and the pom.xml file. You may want to make a copy of the project folder because you will be changing one file that will cause an exception to thrown if you run the project after the change.

Step 4: Alter the fxml file

If you are creating a project with an fxml file in the resources folder it likely contains a reference to its java controller file. In this project the file Scene.fxml contains:

fx:controller=”com.kenfogel.archetypejavafx.controllers.FXMLController”>

You need to replace the part of the package that matches the groupId and artifactId with the matching properties. The change looks like:

fx:controller=”${groupId}.${artifactId}.controllers.FXMLController”>

Step 5: Archetype Resolution or Generating the Archetype

Archetype Resolution is the term that the Maven developers use to describe using Maven to generate the archetype. This task will create a new target folder that contains the archetype version of your code.

Open a command window in the project’s root folder. In my case that is D:\mvnbuild\ArchetypeJavaFX. At the prompt issue the command:

mvn archetype:create-from-project

You may ignore all the warnings.

If all has gone well you will you will have the following folder that is not normally part of a Maven build:

D:\NetBeansProjects\javafxarchetype\target\generated-sources\archetype

Step 6: Make the fxml files filterable

The project in the archetype folder now contains two files named archetype-metadata.xml. This xml file determines which files will go into the archetype and whether or not they get filtered. To filter is to substitute a property with a value. That is why we changed the Scene.fxml file in the previous step. The archetype-metadata.xml files do not show that files in the fxml folder of resources are filterable. Locate these two files, mine are in:

D:\NetBeansProjects\javafxarchetype\target\generated-sources\archetype\src\main\resources\META-INF\maven

And

D:\NetBeansProjects\javafxarchetype\target\generated-sources\archetype\target\classes\META-INF\maven

Edit the files, they are identical, to change:

<fileSet encoding="UTF-8">
   <directory>src/main/resources</directory>
   <includes>
      <include>**/*.fxml</include>
      <include>**/*.css</include>
   </includes>
 </fileSet>

to read

<fileSet filtered="true" encoding="UTF-8">
   <directory>src/main/resources</directory>
   <includes>
      <include>**/*.fxml</include>
      <include>**/*.css</include>
   </includes>
</fileSet>

 

Step 7: Install the Archetype in your repository

Open a command window in your archetype folder and at the prompt enter the following commnd:

D:\NetBeansProjects\javafxarchetype\target\generated-sources\archetype>mvn install

Ignore any warnings. The archetype should be safely in

.m2\repository\com\kfcstandard\javafxarchetype-archetype

where ever your .m2 folder is.

Step 8a: Test if it works with the Maven command line

Create a new folder on your computer for the test. I created D:\mvntest. Open a command window in this folder. Run the command:

D:\mvntest>mvn archetype:generate -DarchetypeCatalog=local

What follows next is pretty straight forward. Here is my output. Notice that you need to enter the groupId, artifactId, version and package. The package should be made up from the groupId and artifactId.

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Stub Project (No POM) 1
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> maven-archetype-plugin:2.4:generate (default-cli) > generate-sources @ standalone-pom >>>
[INFO]
[INFO] <<< maven-archetype-plugin:2.4:generate (default-cli) < generate-sources @ standalone-pom <<<
[INFO]
[INFO] --- maven-archetype-plugin:2.4:generate (default-cli) @ standalone-pom ---
[INFO] Generating project in Interactive mode
[INFO] No archetype defined. Using maven-archetype-quickstart (org.apache.maven.archetypes:maven-archetype-quickstart:1.0)
Choose archetype:
1: local -> com.kfcstandard:javafxarchetype-archetype (Standard starting point for JavaFX programs for students of Ken Fogel)
Choose a number or apply filter (format: [groupId:]artifactId, case sensitive contains): : 1
Define value for property 'groupId': : com.squirrel
Define value for property 'artifactId': : moose
Define value for property 'version': 1.0-SNAPSHOT: :
Define value for property 'package': com.squirrel: : com.squirrel.moose
Confirm properties configuration:
groupId: com.squirrel
artifactId: moose
version: 1.0-SNAPSHOT
package: com.squirrel.moose
 Y: : y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Archetype: javafxarchetype-archetype:0.1
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.squirrel
[INFO] Parameter: artifactId, Value: moose
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: package, Value: com.squirrel.moose
[INFO] Parameter: packageInPathFormat, Value: com/squirrel/moose
[INFO] Parameter: package, Value: com.squirrel.moose
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] Parameter: groupId, Value: com.squirrel
[INFO] Parameter: artifactId, Value: moose
[INFO] project created from Archetype in dir: D:\mvntest\moose
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 30.660 s
[INFO] Finished at: 2015-09-01T20:48:04-04:00
[INFO] Final Memory: 13M/243M
[INFO] ------------------------------------------------------------------------
D:\mvntest>

In the project that has been created in the mvnbuild folder examine the Scene.fxml file and ensure that it has the path to the controller based on the groupId and artifactId you entered. Check out the pom.xml in the project and verify that the mainClass has the proper package.

Step 8b: Testing with NetBeans

 Start NetBeans and select:

File -> New Project

In the dialog choose Maven on the left and Project from Archetype on the right and click on Next.

01NewProject

Scroll down to find javafxarchetype-archetype, select it and click on Next.

02Archetype01

On the next screen you can fill in the Project Name that becomes the Artifact Id, the Group Id, and Version. It’s unusual to change the Package which defaults to Group Id plus Artifact Id.

02Archetype02

Click on Finish and you will a have a project loaded. You should be able to Clean and Build and then Run this project.

03Running

You are now ready to add your code to the project.

Step 8c: Testing with Eclipse

Start Eclipse and then choose File -> New -> Other. Open Maven in this dialog and select Maven Project.

04Eclipse01

There is nothing to change in the next dialog so just click on Next.

04Eclipse02

In the next dialog you should see all the archetypes available to Eclipse. On my system the local archetypes appear first so the archetype we created should be first. Select it and click on Next.

04Eclipse03

Now you can fill in the Group Id, Artifact Id, Version and Package. Like NetBeans the Package is made up of the Group Id and Artifact Id. Click on Finish.

04Eclipse04

The project will appear in the Package Explorer. Right mouse click on the project and choose Run As -> Maven Build and at the dialog just choose Run. The program should open. Close it and you are now ready to add your code to the project.

A Sip of Whine

In learning how to create a maven archetype I was confronted with a serious issue in regards to what I found on the web. Almost every blog, post or stackoverflow answer was nothing more than paraphrasing the Apache Maven documentation. It looks to me these authors have never used Maven or Archetypes in real world situations. The same holds true for the Apache Maven documentation. It lacks any real world examples and is far from being written in plain language.

There is also a bug in the Maven Archetype system. It took me two days to track it down. If you have a package made up of the groupId and artifactId in the src/main/java folder and you have the exact same package in src/test/java then the archetype will be damaged and will not work. The solution was to place the tests in a folder one level below the package. I will report it.

Read the original blog entry...

More Stories By Ken Fogel

In 1980 I bought for myself the most wonderful toy of the day, the Apple ][+. Obsession followed quickly and by 1983 I was writing software for small and medium sized businesses in Montreal for both the Apple and the IBM PC under the company name Omnibus Systems. In the evenings I taught continuing education courses that demystified the computer to the first generation of workers who found themselves with their typewriter on the scrap heap and a PC with WordStar taking its place.

In 1990 I was invited to join the faculty at Dawson College in the Computer Science Technology program. When I joined the program the primary language was COBOL and my responsibility was to teach small systems languages such as BASIC and C/C++.

Today I am now the chairperson and program coordinator of the Computer Science Technology program at Dawson. The program's primary language is Java and the focus is on enterprise programming.

I like to write about the every day problems my students and I face in using various languages and platforms to get the job done. And from time to time I stray from the path and write about what I plan to do, what I actually get around to doing, and what I imagine I am doing.

@omniprof

Latest Stories
As the digitization of business accelerates the move of critical applications and content to the cloud, the network has never been as critical to business success. Consuming everything ‘as-a-service' requires new levels of network automation, agility and security. Discover how Enterprises can take advantage of Digital Platforms, directly connecting to an extensive ecosystem of digital partners and flex their service at the click of a button.
Blockchain has shifted from hype to reality across many industries including Financial Services, Supply Chain, Retail, Healthcare and Government. While traditional tech and crypto organizations are generally male dominated, women have embraced blockchain technology from its inception. This is no more evident than at companies where women occupy many of the blockchain roles and leadership positions. Join this panel to hear three women in blockchain share their experience and their POV on the futu...
Concerns about security, downtime and latency, budgets, and general unfamiliarity with cloud technologies continue to create hesitation for many organizations that truly need to be developing a cloud strategy. Hybrid cloud solutions are helping to elevate those concerns by enabling the combination or orchestration of two or more platforms, including on-premise infrastructure, private clouds and/or third-party, public cloud services. This gives organizations more comfort to begin their digital tr...
Cloud Storage 2.0 has brought many innovations, including the availability of cloud storage services that are less expensive and much faster than previous generations of cloud storage. Cloud Storage 2.0 has also delivered new and faster methods for migrating your premises storage environment to the cloud and the concept of multi-cloud. This session will provide technical details on Cloud Storage 2.0 and the methods used to efficiently migrate from premises-to-cloud storage. This session will als...
In very short order, the term "Blockchain" has lost an incredible amount of meaning. With too many jumping on the bandwagon, the market is inundated with projects and use cases that miss the real potential of the technology. We have to begin removing Blockchain from the conversation and ground ourselves in the motivating principles of the technology itself; whether it is consumer privacy, data ownership, trust or even participation in the global economy, the world is faced with serious problems ...
In a recent survey, Sumo Logic surveyed 1,500 customers who employ cloud services such as Amazon Web Services (AWS), Microsoft Azure, and Google Cloud Platform (GCP). According to the survey, a quarter of the respondents have already deployed Docker containers and nearly as many (23 percent) are employing the AWS Lambda serverless computing framework. It's clear: serverless is here to stay. The adoption does come with some needed changes, within both application development and operations. Th...
For enterprises to maintain business competitiveness in the digital economy, IT modernization is required. And cloud, with its on-demand, elastic and scalable principles has resoundingly been identified as the infrastructure model capable of supporting fast-changing business requirements that enterprises are challenged with, as a result of our increasingly connected world. In fact, Gartner states that by 2022, 28% of enterprise IT spending will have shifted to cloud. But enterprises still must d...
Cloud-Native thinking and Serverless Computing are now the norm in financial services, manufacturing, telco, healthcare, transportation, energy, media, entertainment, retail and other consumer industries, as well as the public sector. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time to wait for long development cycles that pro...
The level of trust we have with individuals, businesses, and technology affects our lives daily. This is important to remember when discussing new technologies. For example, our level of trust is a critical factor when evaluating a new technology as a potential solution for providing business value. Given the importance of trust, imagine one's reaction upon hearing that blockchain is a "trustless trust" system. On the surface, that does sound like an oxymoron. This paper discusses how "trustless...
Public clouds dominate IT conversations but the next phase of cloud evolutions are "multi" hybrid cloud environments. The winners in the cloud services industry will be those organizations that understand how to leverage these technologies as complete service solutions for specific customer verticals. In turn, both business and IT actors throughout the enterprise will need to increase their engagement with multi-cloud deployments today while planning a technology strategy that will constitute a ...
Data center, on-premise, public-cloud, private-cloud, multi-cloud, hybrid-cloud, IoT, AI, edge, SaaS, PaaS... it's an availability, security, performance and integration nightmare even for the best of the best IT experts. Organizations realize the tremendous benefits of everything the digital transformation has to offer. Cloud adoption rates are increasing significantly, and IT budgets are morphing to follow suit. But distributing applications and infrastructure around increases risk, introdu...
Moving to Azure is the path to digital transformation, but not every journey is effective. Organizations that start with a cohesive, well-planned migration strategy can avoid common mistakes and stay a step ahead of the competition. Learn from Atmosera CEO, Jon Thomsen about the opportunities and challenges found in three pivotal phases of the journey to the cloud: Evaluation and Architecting, Migration and Management, and Optimization & Innovation. In each phase, there are distinct insights tha...
Most modern computer languages embed a lot of metadata in their application. We show how this goldmine of data from a runtime environment like production or staging can be used to increase profits. Adi conceptualized the Crosscode platform after spending over 25 years working for large enterprise companies like HP, Cisco, IBM, UHG and personally experiencing the challenges that prevent companies from quickly making changes to their technology, due to the complexity of their enterprise. An accomp...
Every organization is facing their own Digital Transformation as they attempt to stay ahead of the competition, or worse, just keep up. Each new opportunity, whether embracing machine learning, IoT, or a cloud migration, seems to bring new development, deployment, and management models. The results are more diverse and federated computing models than any time in our history.
Andrew Keys is co-founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settlement products to hedge funds and investment banks. After, he co-founded a revenue cycle management company where he learned about Bitcoin and eventually Ethereum.