Sunday, September 30, 2012

Licensing your projects using maven

Well, I have to admit I didn't expect that.

I didn't expect to bump into a Maven plugin that really actually does all the work, so far 100% safe, generic, adaptable, and in less then a day, I've licensed over 250 projects, with different licenses groups, it was very very simple, and straight forward.

I also got something extra in addition to my licensing, implementing it forced me to define my poms better so the groups would share parents, and so on to the top most Maven parent pom, that defines the licensing.

First this is for Java source projects, using the Eclipse IDE, and building projects with Maven, you could also have Java Android projects...

This is the Maven licensing plugin we would be working with.
This is the Maven remote resources plugin we would be working with..

If you would browse a bit in the comments of the first link you would see a comment about the remote-resources plugin, so I gave it a go, worked, like a charm...

Setup:

We would need a Maven License Artifact(project), for the remote resources plugin.
It's pom should look somewhat like this:


<project
 xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.nu.art.software</groupId>
 <artifactId>mm-project-licenser</artifactId>
 <version>0.0.1</version>
 <packaging>jar</packaging>

 <name>_MavenMM-Project-Licenser</name>
 <url>http://maven.apache.org</url>

 <properties>

 </properties>
 
 <build>
  <plugins>
   <plugin>
    <artifactId>maven-remote-resources-plugin</artifactId>
    <version>1.3</version>
    <executions>
     <execution>
      <goals>
       <goal>bundle</goal>
      </goals>
     </execution>
    </executions>
    <configuration>
     <includes>
      <include>nu-art-property-license-v1.0.txt</include>
      <include>mit-license.txt</include>
      <include>apache-license-v2.0.txt</include>
      <include>comment-style-license-header.xml</include>
     </includes>
    </configuration>
   </plugin>
  </plugins>
 </build>
 <dependencies>
 </dependencies>
</project>


In the resources folder of that project I hold the licenses files... and the comment-style I wanted:


apache-license-v2.0.txt:

-----------------------------------------------------------------------------
 Copyright ${founded.year}-${current.year}, '${company}' AND/OR '${owner}' 
 
 For support you may email: <${email}>
   
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-----------------------------------------------------------------------------
 
 Last Builder: ${user.name}


comment-style-license-header.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<additionalHeaders>
    <javadoc_style>
        <firstLine>/*</firstLine>
        <beforeEachLine> * </beforeEachLine>
        <endLine> */</endLine>
        <!--skipLine></skipLine-->
        <firstLineDetectionPattern>(\s|\t)*/\*.*$</firstLineDetectionPattern>
        <lastLineDetectionPattern>.*\*/(\s|\t)*$</lastLineDetectionPattern>
        <allowBlankLines>false</allowBlankLines>
        <isMultiline>true</isMultiline>
    </javadoc_style>
</additionalHeaders>

Next is the licensed project pom, since the license plugin is defined in the top most parent pom, I would post snippets of it:
Some properties:
 <properties>
  <license.header>${license.nu-art}</license.header>
  <license.header.style>${license.header.style.comment}</license.header.style>
  <license.open-source.default>${license.open-source.apache2.0}</license.open-source.default>

  <license.header.style.comment>${project.build.directory}/maven-shared-archive-resources/comment-style-license-header.xml</license.header.style.comment>
  <license.open-source.apache2.0>${project.build.directory}/maven-shared-archive-resources/apache-license-v2.0.txt</license.open-source.apache2.0>
  <license.mit>${project.build.directory}/maven-shared-archive-resources/mit-license.txt</license.mit>
  <license.nu-art>${project.build.directory}/maven-shared-archive-resources/nu-art-property-license-v1.0.txt</license.nu-art>
 </properties>


And a licensing profile:
 <profile>
  <id>add-licenses</id>
  <build>
   <plugins>
    <plugin>
     <groupId>com.mycila.maven-license-plugin</groupId>
     <artifactId>maven-license-plugin</artifactId>
     <version>1.10.b1</version>
     <configuration>
      <basedir>${project.sourceDirectory}</basedir>
      <header>${license.header}</header>
      <failIfMissing>true</failIfMissing>
      <aggregate>true</aggregate>
      <strictCheck>true</strictCheck>
      <properties>
       <owner>${owner} AKA ${nickname}</owner>
       <company>${company.name}</company>
       <founded.year>${company.founded.year}</founded.year>
       <current.year>${current.year}</current.year>
       <email>${company.support}</email>
       <user.name>${user.name}</user.name>
       <timestamp>${maven.build.timestamp}</timestamp>
      </properties>
      <includes>
       <include>**/*.java</include>
      </includes>
      <excludes>
       <exclude>gen/**/R.java</exclude>
       <exclude>gen/**/BuildConfig.java</exclude>
      </excludes>
      <headerDefinitions>
       <headerDefinition>${license.header.style}</headerDefinition>
      </headerDefinitions>
     </configuration>
     <executions>
      <execution>
       <id>Verify-Licenses</id>
       <phase>verify</phase>
       <goals>
        <goal>check</goal>
       </goals>
      </execution>
      <execution>
       <id>Add-Licenses</id>
       <phase>generate-sources</phase>
       <goals>
        <goal>format</goal>
       </goals>
      </execution>
     </executions>
    </plugin>
   </plugins>
  </build>
 </profile>


And a Remote Resources plugin:
 <plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-remote-resources-plugin</artifactId>
  <version>1.3</version>
  <configuration>
   <resourceBundles>
    <resourceBundle>com.nu.art.software:mm-project-licenser:0.0.1</resourceBundle>
   </resourceBundles>
   <attached>false</attached>
  </configuration>
  <executions>
   <execution>
    <phase>generate-sources</phase>
    <goals>
     <goal>process</goal>
    </goals>
   </execution>
  </executions>
 </plugin>

I don't think that there is more to it... if you understand a bit Maven you should be fine together with the links supplied. 

Making Eclipse, Maven and Tomcat to play nice

Hi,

So I've been struggeling with this for hours, till I found one ultimate solution to run and debug a tomcat servlet in Eclipse, so here it is:


First a parent pom should look something like this:

<project
 xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <parent>
  <groupId>com.nu.art.software</groupId>
  <artifactId>nu-art-maven-deploy-project</artifactId>
  <version>0.0.1</version>
 </parent>

 <groupId>com.nu.art.software</groupId>
 <artifactId>tomcat-servlet-maven-parent</artifactId>
 <version>0.0.1</version>
 <packaging>pom</packaging>

 <name>Tomcat Servlet Parent Pom</name>
 <dependencies>
  <dependency>
   <groupId>org.apache.tomcat</groupId>
   <artifactId>tomcat-catalina</artifactId>
   <version>7.0.27</version>
   <scope>provided</scope>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>tomcat-maven-plugin</artifactId>
    <version>1.1</version>
    <configuration>
     <webXml>src/main/webapp/WEB-INF/web.xml</webXml>
    </configuration>
   </plugin>
  </plugins>
  <finalName>${project.name}</finalName>
 </build>
</project>

Note the location of the web.xml & project name...

Next is the child project pom, which should like something like this:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <parent> 
  <artifactId>tomcat-servlet-maven-parent</artifactId>
  <groupId>com.nu.art.software</groupId>
  <version>0.0.1</version> 
 </parent> 
    
    <modelVersion>4.0.0</modelVersion>
 <groupId>com.nu.art.software.cyborg.androidServicesUtility</groupId>
 <artifactId>android-services-utility-server</artifactId>
 <packaging>war</packaging>
 <version>0.1.0</version>
 <name>Android-Services-Utility-Server</name>
 <url>http://maven.apache.org</url>

 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>3.8.1</version>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>com.nu.art.software.arttpServer</groupId>
   <artifactId>arttp-servlet</artifactId>
   <version>0.0.1</version>
   <scope>compile</scope>
  </dependency>
  <dependency>
   <groupId>com.nu.art.software</groupId>
   <artifactId>dbator</artifactId>
   <version>0.0.1</version>
   <type>jar</type>
   <scope>compile</scope>
  </dependency>
  <dependency>
   <groupId>com.nu.art.software.cyborg</groupId>
   <artifactId>cyborg-crash-report-core</artifactId>
   <version>0.0.1</version>
   <type>jar</type>
   <scope>compile</scope>
  </dependency>
  <dependency>
   <groupId>com.nu.art.software.cyborg.server</groupId>
   <artifactId>cyborg-server-module-crash-report</artifactId>
   <version>0.0.1</version>
   <type>jar</type>
  </dependency>
   <dependency>
   <groupId>com.nu.art.software.cyborg.server</groupId>
   <artifactId>cyborg-server-module-c2dm</artifactId>
   <version>0.0.1</version>
   <type>jar</type>
  </dependency>
 </dependencies>
</project>

And finally goto Project -> Properties -> Deployment Assembly, And remove all the content from it. Add the following folder to the assembly: target/${project.name}

This would result in debugging the latest servlet build by Maven. so in order to launch it install your project, and Run/Debug on server...

NOTE!!! 
This is not a perfect working setup, but if you are struggling with the bugger, and want to be able to quickly evaluate your project, this works fine.

Friday, September 21, 2012

Batch search for files - With sub-directories

Well took a while...

Why on earth would I need such a thing? 
Actually I was looking for a solution to map all the *.c & *.cpp in these folders and to add them all to the Android.mk file, I'm trying to integrate aubio native library, and there are tones of files...

Why the Batch?
Sometimes you get carried away with your initial thought, and that's what happened, I started off trying to use the dir command to accomplish this, but it is impotent, then got carried away in Batch-Land...

Since searching into sub-directories is a recursive action, I have implemented this in two batch files, a runner and the search itself.

So here is the runner:

@echo off

set keyPhrase=%1
set yourDir=%~dp0
set outputFileName=output
echo echo output for search > %outputFileName%.bat

call search-in-subdirectories.bat %yourDir% %keyPhrase%
pause
cls call %outputFileName%.bat pause


And here is the search itself (search-in-subdirectories.bat):


echo Dir: %1

for %%a in ("%1\%2") do echo echo %%~fa >> %outputFileName%.bat

for /d %%a in ("%1\*") do call %0 %%~fa %2


Simply locate both runner and search batches in some folder, and call the runner with the search parameter e.g.:


runner.bat *.c


Share your thoughts...

I could always make this better, but it works... and I don't have the time to abstract this further...

Thursday, May 31, 2012

Conversion of numeric primitives to byte array and vice verse

As I was so tired of every now and again to find out how to perform the conversion for different primitives, and then I found this, which is very good because it does the work, but annoying somewhat to over view, every conversion has its own method.

Me been me will not allow myself to write that much of a code for same functionality, I have two generic methods to do all the conversion work for me...

This method would convert any given numeric none floating, to byte array.


  /**
  * Converts any given numeric value which is not floating, to byte array. <br>
  * <br>
  * {@link Byte}<br>
  * {@link Short}<br>
  * {@link Integer}<br>
  * {@link Long}<br>
  *
  * @param _value The value to parse
  * @return A byte array of the supplied value.
  */
 public static byte[] toByteArray(Number _value) {
  int length;
  long value;
  if (_value instanceof Byte) {
   length = Byte.SIZE >> 3;
   value = (Byte) _value;
  } else if (_value instanceof Short) {
   length = Short.SIZE >> 3;
   value = (Short) _value;
  } else if (_value instanceof Integer) {
   length = Integer.SIZE >> 3;
   value = (Integer) _value;
  } else if (_value instanceof Long) {
   length = Long.SIZE >> 3;
   value = (Long) _value;
  } else
   throw new IllegalArgumentException(
     "Parameter must be one of the following types:\n Byte, Short, Integer, Long");
  byte[] byteArray = new byte[length];
  for (int i = 0; i < length; i++) {
   byteArray[i] = (byte) ((value >> (8 * (length - i - 1))) & 0xff);
  }
  return byteArray;
 }

  

This method would convert a byte array to a long value, according to the size of the array.


 /**
  * Converts a byte array, to a numeric value. <br>
  * <br>
  * {@link Byte}<br>
  * {@link Short}<br>
  * {@link Integer}<br>
  * {@link Long}<br>
  *
  * @param byteArray The byte array with the value to parse.
  * @return The value which rests within the supplied byte array
  */
 public static long fromByteArray(byte[] byteArray) {
  int length = byteArray.length;
  long value = 0;

  if (length > 8)
   throw new IllegalArgumentException("Array parameter length==" + byteArray.length + ". MUST be length <= 8");

  for (int i = 0; i < length; i++) {
   value |= ((0xffL & byteArray[i]) << (8 * (length - i - 1)));
  }
  return value;
 }


If you have found this useful, +1 this...


As discussed here, perhaps I should have mentioned that this is a quick generic unified access to the conversion function, that SHOULD not be used with crazy ass repetitive loops...