[Java] Deploy a web application created with Eclipse + Maven + Ontology on Heroku

Introduction

I will summarize how to deploy a web application created with Eclipse + Maven + Ontology on Heroku Regarding the deployment of Heroku, I referred to the following site

Four things that Heroku beginners stumbled upon when deploying a web app created in Java with GitHub integration

Please note that this method is just an example.

The source code is below https://github.com/YasufumiNakama/heroku-java-practice

Contents

Create a web application with Eclipse + Maven + Ontology

Creating a Maven project

I will omit the installation of Eclipse etc. First select the Maven project from Eclipse スクリーンショット 0031-08-09 2.09.19.png Enter the Group ID and Artifact ID スクリーンショット 0031-08-09 3.41.20.png Then, the directory is configured as follows, so let's write the code from here スクリーンショット 0031-08-09 3.43.11.png

Preparation of resources

This time, as resources, I will use an owl file that describes the (ramen) ontology (because there was one that was already created and it was simple and easy). スクリーンショット 0031-08-09 2.34.00.png As a simple example, let's try to fetch a subclass of the "ramen" class by throwing a query and outputting it to html (it was really interesting to fetch something for the user's input). However, since the purpose is to deploy to Heroku, I will cut corners)

Preparation of pom.xml

Since we exchange resources with ontology, we use jena, jersey, and gson as \ <dependencies > in addition to junit used for test.

Also, \ <build > is officially explained, so from here Use the code as is See the source code for more details, as we've made other changes.

Preparation of web.xml

Create src / main / webapp / WEB-INF / web.xml and set jersey Details are as per the source code

java source code

Since I set the group ID to com.heroku-java-practice, I will write the java source code under src / main / java / com / heroku_java_practice (test is under src / test / java / heroku_java_practice) First, I wrote a program called ramen.java that queries and fetches subclasses of the "ramen" class from an owl file that describes (ramen) ontology as resources.

ramen.java


package com.heroku_java_practice;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

import com.hp.hpl.jena.rdf.model.*;
import com.hp.hpl.jena.util.*;
import com.hp.hpl.jena.query.*;

public class ramen {
	
	public static String path = new File(".").getAbsoluteFile().getParent();
	public static String Resources = path + "/src/main/resources/";

    public static HashSet<String> get_ramen() throws FileNotFoundException {
        //Read owl file
        Model model = FileManager.get().loadModel(Resources + "owl/ramen.owl");
        // query
        String queryString = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n" +
        		             "SELECT ?y\n" +
                			 "WHERE{\n" +
                             "?y rdfs:subClassOf <http://www.semanticweb.org/nakamayasufumi/ontologies/2019/5/untitled-ontology-30#ramen>\n" +
                             "}";
        // result
        HashSet<String> result = new HashSet<>();
        //Loading SPARQL queries
        Query query = QueryFactory.create(queryString);
        //SPARQL query execution
        QueryExecution qexec = QueryExecutionFactory.create(query, model);
        try {
            ResultSet results = qexec.execSelect();
            for (; results.hasNext(); ) {
                QuerySolution soln = results.nextSolution();
                Resource y = soln.getResource("y");
                result.add(y.toString().split("#")[y.toString().split("#").length-1]);
            }
        } finally {
            qexec.close();
        }
        return result;
    }
}

Now, let's check with test whether ramen.java is behaving as expected.

RamenTest.java


package heroku_java_practice;

import static org.junit.Assert.*;
import org.junit.Test;

import com.heroku_java_practice.*;

public class RamenTest {
	
	@Test
	public void test() {
		try {
			ramen status = new ramen();
			System.out.println(status.get_ramen());
		} catch(Exception e) {
			e.printStackTrace();
			fail(e.toString());
		}
	}
	
}

When RamenTest.java is executed by JUnit test, the output result is as follows, and it was confirmed that it behaves as expected. スクリーンショット 0031-08-09 3.57.28.png Finally, let's write main.java to reflect this output result on the Web jersey is convenient

main.java


package com.heroku_java_practice;

import java.io.FileNotFoundException;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

@Path("webmethods")
public class main {

	@Path("/ramen")
	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public String return_ramen() throws FileNotFoundException {
		ramen status = new ramen();
		Gson gson = new GsonBuilder().serializeNulls().create();
		String output = gson.toJson(status.get_ramen());
		return output;
	}

}

Let's check the behavior of main.java

RamenTest.java


package heroku_java_practice;

import static org.junit.Assert.*;
import org.junit.Test;

import com.heroku_java_practice.*;

public class RamenTest {
	
	/*
	@Test
	public void test() {
		try {
			ramen status = new ramen();
			System.out.println(status.get_ramen());
		} catch(Exception e) {
			e.printStackTrace();
			fail(e.toString());
		}
	}
	*/
	
	@Test
	public void test() {
		try {
			main status = new main();
			System.out.println(status.return_ramen());
		} catch(Exception e) {
			e.printStackTrace();
			fail(e.toString());
		}
	}
	
}
スクリーンショット 0031-08-09 4.06.25.png Sounds okay

HTML and JavaScript

Create HTML and JavaScript under src / main / webapp /

index.html


<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>project-ramen</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="index.js"></script>
</head>
<body>
    <h2>ramen</h2>
    <table id="ramen"></table>
    <hr style="border-style:dashed">
</body>
</html>

index.js


const MethodURI =  "https://heroku-java-practice.herokuapp.com/webapi/webmethods/";

function gebi(id) {
    return document.getElementById(id);
}

window.onload = function(){
	var uri = MethodURI+'ramen';
	$.getJSON(uri,
	    function(data){
            for(var i=0; i<data.length; i++){
	            var table = gebi("ramen");
	            $(table).append("<tr></tr>").find("tr:last").append("<td>"+"・"+data[i]+"</td>")
	        }
        }
	);
}

With this js you can arrange the previous output in a table format By the way, regarding MethodURI, it is better to secure the App name on Heroku side in advance before writing. Regarding webapi / webmethods /, it will be the one set in web.xml and main.java respectively.

Deploy to Heroku

Create App Secure an App name to deploy スクリーンショット 0031-08-09 4.22.02.png

Before deploying

Before deploying, there are some files needed for deployment

Let's prepare the above files

Push to Github

After adding the above files, push the source code you want to deploy to Github.

Deploy

Here I will try deploying from Github screencapture-dashboard-heroku-apps-heroku-java-practice-deploy-heroku-git-2019-08-09-04_33_57.png Click on the Github icon スクリーンショット 0031-08-09 4.48.35.png It's OK if you link with the Github repository you want to deploy スクリーンショット 0031-08-09 4.50.55.png Either Automatic deploys or Manual deploy will do, so click to deploy スクリーンショット 0031-08-09 6.16.45.png

File structure of deployed application

You can check the file structure of the deployed application as follows.

$ heroku login
$ heroku run bash -a <appName>
$ pwd
/app
$ ls

Reference: [How to see files and file structure on a deployed Heroku app](https://stackoverflow.com/questions/38924458/how-to-see-files-and-file-structure-on-a-deployed-heroku] -app)

in conclusion

For the time being, deploying the Web application created with [Java] Eclipse + Maven + Ontology on Heroku is complete. If there is something else, I may add it (2019/8/9)

Recommended Posts

[Java] Deploy a web application created with Eclipse + Maven + Ontology on Heroku
Deploy a Java web app on Heroku
Try developing a containerized Java web application with Eclipse + Codewind
Deploy a Tomcat-based Eclipse project on Heroku
Deploy Java web app to Azure with maven
[Tutorial] Download Eclipse → Run Web application with Java (Pleiades)
How to deploy a simple Java Servlet app on Heroku
Four things Heroku stumbled upon when deploying a web app created in Java with GitHub
Deploy a Docker application with Greengrass
Deploy a war file on Heroku
Build a web application with Javalin
Automatically deploy a Web application developed in Java using Jenkins [Preparation]
Create a simple web application with Dropwizard
Build Web Application Server (Java) on VPS
Creating a java web application development environment with docker for mac part1
Automatically deploy a web application developed in Java using Jenkins [Spring Boot application]
Volume of trying to create a Java Web application on Windows Server 2016
Create a java web application development environment with docker for mac part2
[LINE BOT] I made a ramen BOT with Java (Maven) + Heroku + Spring Boot (1)
[Java / Eclipse / Servlet / JSP / PostgreSQL] A WEB application framework with data posting / saving / editing / updating / deleting functions
[AWS] How to automatically deploy a Web application created with Rails 6 to ECR / ECS using CircleCI ① Preparation [Container deployment]
How to deploy a system created with Java (Wicket-Spring boot) to an on-campus server
Run an application made with Go on Heroku
Java Repository of Eclipse with Maven: Missing artifact ~
Deploy a Spring Boot application on Elastic Beanstalk
[For beginners] Until building a Web application development environment using Java on Mac OS
A story that stumbled when deploying a web application created with Spring Boot to EC2
Create a Java (Maven) project with VS Code and develop it on a Docker container
Submit a job to AWS Batch with Java (Eclipse)
[Tutorial] Download Eclipse → Run the application with Java (Pleiades)
Compile with Java 6 and test with Java 11 while running Maven on Java 8
Create a JAVA WEB application and try OMC APM
Deploy a Java application developed locally with the Cloud Toolkit to an Alibaba Cloud ECS instance
Story of making a task management application with swing, java
I tried to modernize a Java EE application with OpenShift.
Java web application development environment construction with VS Code (struts2)
Deploy the application created by Spring Boot to Heroku (public) ①
How to deploy a kotlin (java) app on AWS fargate
Until you create a Web application with Servlet / JSP (Part 1)
How to deploy Java application to Alibaba Cloud EDAS in Eclipse
Socket communication with a web browser using Java and JavaScript ②
Socket communication with a web browser using Java and JavaScript ①
Java: Start WAS with Docker and deploy your own application
How to deploy a Rails application on AWS (article summary)
Create a simple DRUD application with Java + SpringBoot + Gradle + thymeleaf (1)
AWS Elastic Beanstalk # 1 with Java starting from scratch-Building a Java web application environment using the EB CLI-
How to deploy on heroku
Try gRPC with Java, Maven
Web application built with docker (1)
Create a simple web server with the Java standard library com.sun.net.httpserver
Comparison of WEB application development with Rails and Java Servlet + JSP
Let's make a book management web application with Spring Boot part1
Let's make a book management web application with Spring Boot part3
Let's make a book management web application with Spring Boot part2
Automatically deploy Web applications developed in Java using Jenkins [Tomcat application]
[Probably the easiest] WEB application development with Apache Tomcat + Java Servlet
[Spring Boot] Precautions when developing a web application with Spring Boot and placing war on an independent Tomcat server
Build a web application development environment that uses Java, MySQL, and Redis with Docker CE for Windows
The first thing to do when you want to be happy with Heroku on GitHub with Eclipse in Java
# 1 [Beginner] Create a web application (website) with Eclipse from knowledge 0. "Let's build an environment for creating web applications"