Tuesday, June 24, 2014

Test command on file size in bash script

My objective was to capture the files locate in a particular path into a file, this is what I did in my bash script.
 # find out all available file
 files=$(ls -1 ${SRC_PATH}/*.txt)
 echo "${files}" > ${TEMP_FILE} 

 # convert the space between each file into newline
 sed 's/ /\n/g' ${TEMP_FILE} > ${PARSE_FILE}
 
 # loop through each file and check the file size is greater than zero
 for file in `cat ${PARSE_FILE}`; do

  file_size=`ls -lah ${file} | awk '{ print $5 }'`
  if [ $file_size -ne 0 ]; then
   echo $file >> "${NICE_FILE}"
  fi
 done
After a few round of review and unit testing on my code, I realize there is a problem where sed command was unable to handle file name with space within it. After a few round of rework, finally I came out a so called "perfect" solution.
 # find out all available file
 files=$(ls -1 ${SRC_PATH}/*.txt)
 echo "${files}" > ${TEMP_FILE} 

 # loop through each file and check the file size is greater than zero
 while read file 
 do
  if [ -s "${file}" ]
  then
   newfile=$(echo "${file}" | sed 's, ,\\ ,g')
    
   echo "${newfile}" >> "${NICE_FILE}"
  fi
 done < ${TEMP_FILE}
No, this is not yet the perfect solution, I'm sure there is always a better one beyond this universe.

Friday, June 20, 2014

Resolving log4j 1.2.15 dependencies in Maven

Recently I was configuring log4j 1.2.15 in Maven and I found it weird when Maven complaining this in my pom.xml.
ArtifactTransferException: Failure to transfer com.sun.jdmk:jmxtools:jar:1.2.1 from https://maven-repository.dev.java.net/nonav/repository was cached in the local repository, resolution will not be reattempted until the update interval of java.net has elapsed or updates are forced. Original error: Could not transfer artifact com.sun.jdmk:jmxtools:jar:1.2.1 from/to java.net (https://maven-repository.dev.java.net/nonav/repository): No connector available to access repository java.net (https://maven-repository.dev.java.net/nonav/repository) of type legacy using the available factories AsyncRepositoryConnectorFactory, WagonRepositoryConnectorFactory
ArtifactTransferException: Failure to transfer com.sun.jmx:jmxri:jar:1.2.1 from https://maven-repository.dev.java.net/nonav/repository was cached in the local repository, resolution will not be reattempted until the update interval of java.net has elapsed or updates are forced. Original error: Could not transfer artifact com.sun.jmx:jmxri:jar:1.2.1 from/to java.net (https://maven-repository.dev.java.net/nonav/repository): No connector available to access repository java.net (https://maven-repository.dev.java.net/nonav/repository) of type legacy using the available factories AsyncRepositoryConnectorFactory, WagonRepositoryConnectorFactory
ArtifactTransferException: Failure to transfer javax.jms:jms:jar:1.1 from https://maven-repository.dev.java.net/nonav/repository was cached in the local repository, resolution will not be reattempted until the update interval of java.net has elapsed or updates are forced. Original error: Could not transfer artifact javax.jms:jms:jar:1.1 from/to java.net (https://maven-repository.dev.java.net/nonav/repository): No connector available to access repository java.net (https://maven-repository.dev.java.net/nonav/repository) of type legacy using the available factories AsyncRepositoryConnectorFactory, WagonRepositoryConnectorFactory
Missing artifact com.sun.jdmk:jmxtools:jar:1.2.1
Missing artifact com.sun.jmx:jmxri:jar:1.2.1
Missing artifact javax.jms:jms:jar:1.1
Googling around found out that log4j is having dependencies on jmxri, jmxtools, and jms. Unfortunately there are some licensing issue from SUN that I must exclude those dependencies when retrieving log4j from Maven. This is how things get done in my pom.xml.
        
         log4j
         log4j
         1.2.15
         
          
           com.sun.jdmk
           jmxtools
       
       
               javax.jms
               jms
       
       
        com.sun.jmx
           jmxri