Skip to content

Commit c8ce475

Browse files
committed
Initial commit
1 parent a8e3999 commit c8ce475

File tree

23 files changed

+1141
-3
lines changed

23 files changed

+1141
-3
lines changed

.gitignore

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,32 @@
1313
# Package Files #
1414
*.jar
1515
*.war
16-
*.nar
1716
*.ear
1817
*.zip
1918
*.tar.gz
2019
*.rar
2120

21+
# Maven
22+
target/
23+
2224
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
2325
hs_err_pid*
26+
27+
# Intellij
28+
*.iml
29+
.idea/
30+
31+
# Eclipse
32+
*.pydevproject
33+
.project
34+
.metadata
35+
tmp/
36+
*.tmp
37+
*.bak
38+
*.swp
39+
*~.nib
40+
local.properties
41+
.classpath
42+
.settings/
43+
.loadpath
44+
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
Licensed to the Apache Software Foundation (ASF) under one
3+
or more contributor license agreements. See the NOTICE file
4+
distributed with this work for additional information
5+
regarding copyright ownership. The ASF licenses this file
6+
to you under the Apache License, Version 2.0 (the
7+
"License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
https://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
KIND, either express or implied. See the License for the
16+
specific language governing permissions and limitations
17+
under the License.
18+
*/
19+
20+
import java.io.File;
21+
import java.io.FileInputStream;
22+
import java.io.FileOutputStream;
23+
import java.io.IOException;
24+
import java.net.URL;
25+
import java.nio.channels.Channels;
26+
import java.nio.channels.ReadableByteChannel;
27+
import java.util.Properties;
28+
29+
public class MavenWrapperDownloader {
30+
31+
/**
32+
* Default URL to download the maven-wrapper.jar from, if no 'downloadUrl' is provided.
33+
*/
34+
private static final String DEFAULT_DOWNLOAD_URL =
35+
"https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.4.2/maven-wrapper-0.4.2.jar";
36+
37+
/**
38+
* Path to the maven-wrapper.properties file, which might contain a downloadUrl property to
39+
* use instead of the default one.
40+
*/
41+
private static final String MAVEN_WRAPPER_PROPERTIES_PATH =
42+
".mvn/wrapper/maven-wrapper.properties";
43+
44+
/**
45+
* Path where the maven-wrapper.jar will be saved to.
46+
*/
47+
private static final String MAVEN_WRAPPER_JAR_PATH =
48+
".mvn/wrapper/maven-wrapper.jar";
49+
50+
/**
51+
* Name of the property which should be used to override the default download url for the wrapper.
52+
*/
53+
private static final String PROPERTY_NAME_WRAPPER_URL = "wrapperUrl";
54+
55+
public static void main(String args[]) {
56+
System.out.println("- Downloader started");
57+
File baseDirectory = new File(args[0]);
58+
System.out.println("- Using base directory: " + baseDirectory.getAbsolutePath());
59+
60+
// If the maven-wrapper.properties exists, read it and check if it contains a custom
61+
// wrapperUrl parameter.
62+
File mavenWrapperPropertyFile = new File(baseDirectory, MAVEN_WRAPPER_PROPERTIES_PATH);
63+
String url = DEFAULT_DOWNLOAD_URL;
64+
if(mavenWrapperPropertyFile.exists()) {
65+
FileInputStream mavenWrapperPropertyFileInputStream = null;
66+
try {
67+
mavenWrapperPropertyFileInputStream = new FileInputStream(mavenWrapperPropertyFile);
68+
Properties mavenWrapperProperties = new Properties();
69+
mavenWrapperProperties.load(mavenWrapperPropertyFileInputStream);
70+
url = mavenWrapperProperties.getProperty(PROPERTY_NAME_WRAPPER_URL, url);
71+
} catch (IOException e) {
72+
System.out.println("- ERROR loading '" + MAVEN_WRAPPER_PROPERTIES_PATH + "'");
73+
} finally {
74+
try {
75+
if(mavenWrapperPropertyFileInputStream != null) {
76+
mavenWrapperPropertyFileInputStream.close();
77+
}
78+
} catch (IOException e) {
79+
// Ignore ...
80+
}
81+
}
82+
}
83+
System.out.println("- Downloading from: : " + url);
84+
85+
File outputFile = new File(baseDirectory.getAbsolutePath(), MAVEN_WRAPPER_JAR_PATH);
86+
if(!outputFile.getParentFile().exists()) {
87+
if(!outputFile.getParentFile().mkdirs()) {
88+
System.out.println(
89+
"- ERROR creating output direcrory '" + outputFile.getParentFile().getAbsolutePath() + "'");
90+
}
91+
}
92+
System.out.println("- Downloading to: " + outputFile.getAbsolutePath());
93+
try {
94+
downloadFileFromURL(url, outputFile);
95+
System.out.println("Done");
96+
System.exit(0);
97+
} catch (Throwable e) {
98+
System.out.println("- Error downloading");
99+
e.printStackTrace();
100+
System.exit(1);
101+
}
102+
}
103+
104+
private static void downloadFileFromURL(String urlString, File destination) throws Exception {
105+
URL website = new URL(urlString);
106+
ReadableByteChannel rbc;
107+
rbc = Channels.newChannel(website.openStream());
108+
FileOutputStream fos = new FileOutputStream(destination);
109+
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
110+
fos.close();
111+
rbc.close();
112+
}
113+
114+
}

.mvn/wrapper/maven-wrapper.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.6.0/apache-maven-3.6.0-bin.zip

README.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,42 @@
1-
# Spring-Data-ElasticSearch-Example
2-
An example to demonstrate how Spring Data ElasticSearch works
1+
# Spring Data ElasticSearch Example
2+
3+
## Introduction
4+
5+
This example demonstrates how to use Spring Data ElasticSearch to do simple CRUD operation.
6+
7+
You can find the tutorial about this example at the below link:
8+
9+
[https://blog.madadipouya.com](https://blog.madadipouya.com)
10+
11+
For this example, a Book controller created that allows to do the following operations with ElasticSearch:
12+
13+
- Get list of all books
14+
- Create a book
15+
- Update a book by Id
16+
- Delete a book by Id
17+
- Search for a book by ISBN
18+
19+
20+
## How to run
21+
22+
The first thing to do is to start ElasticSearch. For that you can use the `docker-compose` file in this project
23+
and run it like this:
24+
25+
```bash
26+
$ docker-compose -f docker-compose up -d
27+
```
28+
29+
It brings ElasticSearch up on a single node cluster with the cluser name `elasticsearch`.
30+
31+
Then you can run the application like below:
32+
33+
```bash
34+
$ ./mvnw spring-boot:run
35+
```
36+
37+
If your ElasticSearch URI is not `localhost` and/or the cluster name is different simply override one or both of the following environment variable:
38+
39+
- `ES_URI`
40+
- `ES_CLUSTER_NAME`
41+
42+
Once everything is up and running open the browser and go to [http://localhost:8080](http://localhost:8080). You should see Swagger to interact with.

docker-compose.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
elasticsearch:
2+
image: elasticsearch:6.4.3
3+
container_name: elasticsearch
4+
ports:
5+
- "9300:9300"
6+
- "9200:9200"
7+
environment:
8+
- discovery.type=single-node
9+
- cluster.name=elasticsearch

0 commit comments

Comments
 (0)