Sunday, November 1, 2020

How to easily embed a Google Map in your website

Some websites need a map to better describe the location, eg, Contact Us.

There is an easy way to embed a live Google Map with marker to our website.


Steps:

1. Navigate to https://www.embedgooglemap.net/ 

2. Enter an address into search box. eg. Legoland Malaysia


3. Click on get HTML-Code

4. Copy and paste the piece of HTML code into your HTML. 


Done!!

Wednesday, March 4, 2020

How to load file(s) in the same directory

The code below demonstrates how to load file(s), eg configuration, properties in the same directory with the jar file or the running class file.


try {
    File jarLoc = new File(
                        MyClass.class.getProtectionDomain()
                        .getCodeSource().getLocation().toURI().getPath());

    if(!jarLoc.isDirectory()) {
        // if this is a jar file, the full path with jar name returned. 
        currentPath = jarLoc.getParentFile().getPath();
    } else {
        // if this is a class file, full path until the parent directory
        currentPath = jarLoc.getPath();
    }

    String pathToFile = "currentPath + fileName.fileExtension";

} catch (URISyntaxException e) {
    e.printStackTrace();
}


The code below demonstrates how to load file's content in the jar file or in the classpath.

        InputStream in = getClass().getResourceAsStream(path);
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        String line = null;
        String content = "";
        while ((line = reader.readLine()) != null) {
            content += line;
        }

OR

new String(Files.readAllBytes(Paths.get(path)))




Done!!

Wednesday, December 25, 2019

How to build uber jar (far jar) with Maven

Example below shows how to build uber jar with Maven


<!-- building uber jar --><plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.1.0</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <outputDirectory>${basedir}/path/to/output/directory</outputDirectory>
        <!-- false to keep the original output jar name -->
        <appendAssemblyId>false</appendAssemblyId>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id> <!-- this is used for inheritance merges -->            <phase>package</phase> <!-- bind to the packaging phase -->            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>


Done!!

Tuesday, December 24, 2019

How to copy file(s) with Maven

Example shows how to copy file(s) with Maven


<!-- copy files --><plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <id>copy-config</id>
            <!-- here the phase you need -->            <phase>install</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${basedir}/target/directory</outputDirectory>
                <resources>
                    <resource>
                        <filtering>true</filtering>
                        <directory>src/source/directory</directory>
                        <includes>
                            <include>file_to_copy</include>
                        </includes>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>


Build!!

Monday, December 23, 2019

How to copy folder with Maven

Example below shows how to copy a directory during Maven build. 


<build>

    <plugins>

        <!-- copy directory -->        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <id>copy-resources</id>
                    <!-- here the phase you need -->                    <phase>install</phase>
                    <goals>
                        <goal>copy-resources</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${basedir}/path/to/target/directory</outputDirectory>
                        <resources>
                            <resource>
                                <directory>src/path/to/source/directory</directory>
                                <filtering>true</filtering>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

    </plugins>

</build>


Build!!

LinkWithin

Related Posts Plugin for WordPress, Blogger...