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!!

Sunday, December 22, 2019

How to exclude all files in resources folder

Normally we have some files in the resources folder for our java project(s)
in some scenarios, those files are not to be included in the final jar
so the configuration below demonstrates how to exclude all files in resources with Maven.

<build>

    <!-- exclude all files in resources-->    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>**/**</exclude>
            </excludes>
            <filtering>false</filtering>
        </resource>
    </resources>

    <!-- other configurations/plugins in the pom.xml-->

</build>


Done!! 

Monday, November 4, 2019

How to execute command from Java in Windows



public static void main(String[] args) {
  
  String command = "";
  Process process  = Runtime.getRuntime().exec(command);

  // thread waiting for error
  StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "ERROR");

  // thread waiting for output
  StreamGobbler outputGobbler = new StreamGobbler(process.getInputStream(), "OUTPUT");

  errorGobbler.start();
  outputGobbler.start();

  // wait for process to finish
  p.waitFor();
  p.destroy();
}

 class StreamGobbler extends Thread {

   InputStream is;
   String type;
   StringBuffer buffer = new StringBuffer();

   StreamGobbler(InputStream is, String type) {
     this.is = is;
     this.type = type;
   }

   public void run() {
     InputStreamReader isr = null;
     BufferedReader br = null;
     try {

       isr = new InputStreamReader(is);
       br = new BufferedReader(isr);
       String line = null;
       while ((line = br.readLine()) != null) {
         if (buffer != null) {
           buffer.append(line).append("\n");
         }
         System.out.println(type + ">" + line);
       }
     } catch (IOException ioe) {
       e.printStackTrace();
     } finally {
       try {
         br.close();
         isr.close();
       } catch (IOException e) {
         e.printStackTrace();
       }
     }
  }


Done!!

LinkWithin

Related Posts Plugin for WordPress, Blogger...