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
Please note that this method is just an example.
The source code is below https://github.com/YasufumiNakama/heroku-java-practice
I will omit the installation of Eclipse etc. First select the Maven project from Eclipse Enter the Group ID and Artifact ID Then, the directory is configured as follows, so let's write the code from here
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). 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)
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.
Create src / main / webapp / WEB-INF / web.xml and set jersey Details are as per the 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. 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());
}
}
}
Sounds okay
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.
Create App Secure an App name to deploy
Before deploying, there are some files needed for deployment
Let's prepare the above files
After adding the above files, push the source code you want to deploy to Github.
Here I will try deploying from Github Click on the Github icon It's OK if you link with the Github repository you want to deploy Either Automatic deploys or Manual deploy will do, so click to deploy
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)
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