Java, Groovy on Grails, PHP, Kohana

Блог о программировании на Java, Groovy on Grails, PHP, kohana

Currently Viewing Posts in Java

Spring Boot Build Command Line App

spring-boot-logogradle-logo
console

Create Gradle Spring Boot project with SPRING INITIALIZR

I chose Groovy language for the project, but if you want Java, text below with little changes also helps you.

Edit build.gradle and add to it bootRun section

buildscript {
	ext {
		springBootVersion = '1.3.1.RELEASE'
	}
	repositories {
		mavenCentral()
	}
	dependencies {
		classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
	}
}

apply plugin: 'groovy'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar {
	baseName = 'myapp'
	version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
	mavenCentral()
}


dependencies {
	compile('org.codehaus.groovy:groovy')
	testCompile('org.springframework.boot:spring-boot-starter-test')
}

task wrapper(type: Wrapper) {
	gradleVersion = '2.9'
}

// this code split parameters and allow us use parameters in our application
bootRun {
	if ( project.hasProperty('args') ) {
		args project.args.split('\\s+')
	}
}

create MyController class

@Controller
class MyController implements CommandLineRunner {

	private Logger logger = LoggerFactory.getLogger(this.getClass())

	@Override
	void run(String... args) throws Exception {
		if(!args) {
			logger.warn("You must pass at least one parametr.")
			logger.warn("Example: bootRun -Pargs=\"arg1 arg2 arg3\"") 
		}
		// print parameters which we sent from command line
		args.each{ arg -> 
			logger.info(arg)
		} 
	} 
}

Run application
gradle bootRun -Pargs=”arg1 arg2 arg3″

Java Compare Strings Depends On Locale

Для правильной сортировки строк на украинском языке или любом другом, необходимо явно указывать локаль Locale. Для решения данной проблемы используем класс пакета java.text – Collator.

ArrayList<String> countries = new ArrayList<String>();
countries.add("Єгипет");
countries.add("Туреччина");		
countries.add("Ісландія");		
countries.add("Аргентина");
		

System.out.println("Before sort:");
		
System.out.println(countries);
		

System.out.println("After regular sort:");
		
Collections.sort(countries);
		
System.out.println(countries);
		

Locale ukrainian = new Locale("uk", "UA");
		
Collator ukrainianCollator = Collator.getInstance(ukrainian);

Collections.sort(countries, ukrainianCollator);


System.out.println("After locale sort:");

System.out.println(countries);

Results

Before sort:
[Єгипет, Туреччина, Ісландія, Аргентина]
After regular sort:
[Єгипет, Ісландія, Аргентина, Туреччина]
After locale sort:
[Аргентина, Єгипет, Ісландія, Туреччина]
  • Recent Posts