Groovy 全面的Maven 2存储库配置文件
<profile>
<id>extra-repos</id>
<repositories>
<repository>
<id>central</id>
<name>Maven Repository Switchboard</name>
<url>http://repo1.maven.org/maven2</url>
<releases>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</snapshots>
</repository>
<repository>
<id>maven2-repository.dev.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2</url>
<releases>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</snapshots>
</repository>
<repository>
<id>maven-repository.dev.java.net</id>
<url>
https://maven-repository.dev.java.net/nonav/repository
</url>
<layout>legacy</layout>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>ibiblio-public-repository</id>
<name>Ibiblio</name>
<url>http://www.ibiblio.org/maven2</url>
<releases>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</snapshots>
</repository>
<repository>
<id>codehaus-repo</id>
<name>CodeHaus Repository</name>
<url>http://repository.codehaus.org</url>
<releases>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</snapshots>
</repository>
<repository>
<id>apache-incubating</id>
<name>Apache M2 Incubating Repository</name>
<url>
http://people.apache.org/repo/m2-incubating-repository
</url>
<releases>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</snapshots>
</repository>
<repository>
<id>codehaus-snapshots</id>
<name>CodeHaus Snapshots Repository</name>
<url>http://snapshots.repository.codehaus.org</url>
<releases>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</snapshots>
</repository>
<repository>
<id>apache.snapshots</id>
<name>Maven Plugin Snapshots</name>
<url>
http://people.apache.org/maven-snapshot-repository
</url>
<releases>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</snapshots>
</repository>
<repository>
<id>ibiblio-mirror-m2</id>
<name>Ibiblio Mirror Maven 2</name>
<url>
http://mirrors.ibiblio.org/pub/mirrors/maven2
</url>
<releases>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</snapshots>
</repository>
<repository>
<id>jboss</id>
<url>http://repository.jboss.com/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>jboss-snapshot</id>
<url>http://snapshots.jboss.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>lunarlogic</id>
<url>
http://labs.lunarlogic.com/proximity/repository/lunarlogic
</url>
</repository>
<repository>
<id>jboss-repo</id>
<url>http://repository.jboss.org/maven2</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>maven2-repository.dev.java.net</id>
<url>
https://maven2-repository.dev.java.net/nonav/repository
</url>
<releases>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>maven-repository.dev.java.net</id>
<url>
https://maven-repository.dev.java.net/nonav/repository
</url>
<layout>legacy</layout>
<releases>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>codehaus.org</id>
<name>CodeHaus Plugin Snapshots</name>
<url>http://snapshots.repository.codehaus.org</url>
<releases>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>apache.org</id>
<name>Maven Plugin Snapshots</name>
<url>
http://people.apache.org/maven-snapshot-repository
</url>
<releases>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>central</id>
<name>Maven Plugin Repository</name>
<url>http://repo1.maven.org/maven2</url>
<releases>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>codehaus.plugin.snapshots</id>
<name>CodeHaus Plugin Snapshots</name>
<url>
http://snapshots.maven.codehaus.org/maven2
</url>
<releases>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>ibiblio-mirror-m2</id>
<name>Ibiblio Mirror Maven 2</name>
<url>
http://mirrors.ibiblio.org/pub/mirrors/maven2
</url>
<releases>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>interval:240</updatePolicy>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>jboss-plugins</id>
<url>http://repository.jboss.com/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>jboss-snapshot-plugins</id>
<url>http://snapshots.jboss.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
Groovy Mediator设计模式Groovy实现
// The mediator class which connects all the colleges (buttons in this case)
// and updates them all in reaction to informations from any of them
package design.patterns.mediator
public class Mediator {
def show
def buttons = []
void registerButton(b) {
buttons << b
}
void registerDisplay(d) {
show = d
}
void mediate(disable, text) {
buttons.each {
if (it == disable) {
it.enabled - false
}
else {
it.enabled = true
}
}
show.setText(text)
}
}
// The demo UI which demonstrates the usage
package design.patterns.mediator.ui
import groovy.swing.SwingBuilder
import design.patterns.mediator.Mediator
public class MediatorDemo {
public static void main(String[] args) {
def Mediator mediator = new Mediator()
def frame = new SwingBuilder().frame() {
panel() {
label(text: 'just starting') {mediator.registerDisplay(current)}
button() {
mediator.registerButton(current)
action(name: 'view', closure: {mediator.mediate(current, 'viewing ..')})
}
button() {
mediator.registerButton(current)
action(name: 'book', closure: {mediator.mediate(current, 'booking ..')})
}
button() {
mediator.registerButton(current)
action(name: 'search', closure: {mediator.mediate(current, 'searching ..')})
}
}
}
frame.setSize(400, 200);
frame.setVisible(true);
}
}
Groovy CSV格式化程序
class CsvFormat {
def convertListToCsv(list) {
def result = ''
list.each{row ->
row.each{col -> result += col + ','}
result = result[0..-2]
result += '\n'
}
result
}
}
class CsvFormatTest extends GroovyTestCase {
void testConvertsListToCsv() {
assertEquals '1,2\n3,4\n', new CsvFormat().convertListToCsv([[1,2],[3,4]])
}
}
Groovy 表集合
class Table {
def colNames = []
def rowNames = []
def cells = [:]
def topLeft = ''
def addCol(colName) {
if (!(colName in colNames)) colNames << colName
}
def addRow(rowName) {
if (!(rowName in rowNames)) rowNames << rowName
}
def get(rowName, colName) {
cells.get( computeKey(rowName, colName) )
}
def put(rowName, colName, value) {
if (!(rowName in rowNames)) rowNames << rowName
if (!(colName in colNames)) colNames << colName
def endValue = value
if (value instanceof Closure) {
endValue = value.call(get(rowName, colName))
}
cells.put(computeKey(rowName, colName), endValue)
}
def fill(value) {
rowNames.each{rowName ->
colNames.each{colName -> put(rowName, colName, value)}
}
}
def fillIfNull(value) {
rowNames.each{rowName ->
colNames.each{colName -> put(rowName, colName){ it ?: value } }
}
}
def getRowCount() {
rowNames.size()
}
def getColCount() {
colNames.size()
}
def toListOfLists(withRowNames=true, withColNames=true) {
def result = withColNames ? [[topLeft, *colNames]] : []
rowNames.each{rowName ->
def row = withRowNames ? [rowName] : []
colNames.each{colName ->
row << get(rowName, colName)
}
result << row
}
result
}
def each(Closure closure) {
rowNames.each{rowName ->
colNames.each{colName ->
closure.call(rowName, colName, get(rowName, colName))
}
}
}
private computeKey(rowName, colName) {
[rowName, colName]
}
}
class TableTests extends GroovyTestCase {
def table = new Table(colNames: ['c1', 'c2'], rowNames: ['r1', 'r2'])
void testHasNullAsDefaultCellValue() {
assertNull table.get('r1', 'c1')
}
void testCountsRows() {
assertEquals 2, table.rowCount
}
void testCountsColumns() {
assertEquals 2, table.colCount
}
void testAddsRow() {
table.addRow('r3')
assertEquals 3, table.rowCount
}
void testAddsColumn() {
table.addCol('c3')
assertEquals 3, table.colCount
}
void testHasUniqueColumnNames() {
table.addCol('c1')
assertEquals(['c1', 'c2'], table.colNames)
}
void testHasUniqueRowNames() {
table.addRow('r1')
assertEquals(['r1', 'r2'], table.rowNames)
}
void testAddsColumnOnDemand() {
table.put('r1', 'c3', '')
assertEquals(['c1', 'c2', 'c3'], table.colNames)
}
void testAddsRowOnDemand() {
table.put('r3', 'c1', '')
assertEquals(['r1', 'r2', 'r3'], table.rowNames)
}
void testHasNullDefaultValueInCells() {
assertNull table.get('r1', 'c1')
}
void testPutsValueInCell() {
table.put('r1', 'c1', 'value')
assertEquals 'value', table.get('r1', 'c1')
}
void testPutsClosureResultInCell() {
table.put('r1', 'c1', 'value')
table.put('r1', 'c1'){ it + '2'}
assertEquals 'value2', table.get('r1', 'c1')
}
void testFillsCells() {
table.fill(1)
assertEquals( [[1, 1], [1, 1]], table.toListOfLists(false, false) )
}
void testFillsNullCells() {
table.put('r1', 'c1', 2)
table.fillIfNull(1)
assertEquals( [[2, 1], [1, 1]], table.toListOfLists(false, false) )
}
void testConvertsContentToListOfLists() {
table.put('r1', 'c1', 'r1c1')
table.put('r1', 'c2', 'r1c2')
table.put('r2', 'c1', 'r2c1')
table.put('r2', 'c2', 'r2c2')
table.topLeft = 'x'
assertEquals( [['x', 'c1', 'c2'], ['r1', 'r1c1', 'r1c2'], ['r2', 'r2c1', 'r2c2']], table.toListOfLists() )
}
void testIteratesOverCells() {
table.fill(1)
def result = []
table.each{rowName, colName, value -> result << [rowName, colName, value]}
assertEquals( [['r1', 'c1', 1], ['r1', 'c2', 1], ['r2', 'c1', 1], ['r2', 'c2', 1]], result)
}
}
Groovy 在运行时将jar添加到类路径
this.getClass().classLoader.rootLoader.addURL(new File("file.jar").toURL())
Groovy 排序地图的例子
def sortedCount = badSnpsMap.keySet().toList()
sortedCount.sort{ badSnpsMap[it] }
println ""
println "Bad SNPS count"
println "-------------------------------------------------"
sortedCount.reverse().each { probeSetId ->
println "" << probeSetId << "\t\t" << badSnpsMap[probeSetId]
}
your: 2for: 1
"""
recipes: 1
Groovy 循环索引
// index started with 0
def x= [1,2,3,445,6];
x.eachWithIndex{ obj, index ->
println("${index}. ${obj}");
}
Groovy 在Grails服务上调用会话
/// for example in your controller you write : session.user = "wysmedia" ;
/// you can retrive the value on grails service using this following way.
import javax.servlet.http.HttpSession
import org.springframework.web.context.request.RequestContextHolder
class SessionStorageService {
static transactional = false
static scope = "singleton"
def setUser(User user) {
getSession().user = user
}
def getUser() {
getSession().user
}
private HttpSession getSession() {
return RequestContextHolder.currentRequestAttributes().getSession()
}
}
Groovy Grails:如何在不使用g:view的情况下读取错误(在控制器/服务中)
// in controller/services :
class GrailsService {
boolean transactional = true
def messageSource ;
def locale = Locale.getDefault()
domainObj.errors.each { fieldErrors ->
fieldErrors.allErrors.each { error ->
log.warn messageSource.getMessage(error, locale)
}
}
}
Groovy vim:代码格式化
For groovy : change syntax to Java .. make sure you have ; so it becomes like java. I myself always adding ; on each line of code.
after that
ESC gg = SHIFT-G
means:
1. ESC --> back to mode
2. gg --> go to first line
3. = --> indenting
4. SHIFT G --> to end of file.
for spesific lines
ESC SHIFT V (select the lines) press =
SHIFT V = enter visual mode then you can select which lines you want to format
after that pressing = will format the code