Why use Maven
Maven like ant is a build tool, but unlike ant, maven can be set-up to use a common repository, these repositories contain common jars. Maven uses meta data files called .pom files, pom stands for Project Object Model, these files define each jar. This meta data is also held in the repository, One distinct advantage of using maven over ant is that you only have to define your immediate dependancies in your own pom file. if these dependancies depend other .jars, these will be pulled in automatically. The pom defines what the project is and what it requires, it does not define how to do anything. If these jars are not available in your companies repository maven can be set up to look in alternative repositories, these repositories exist on the web .
How to deploy to the Maven Repository
mvn deploy -Dtest=skip -DfailIfNoTests=false
How to update to the latest parent pom
updates all your parent poms to the latest release version all your parent dependencies
mvn versions:update-parent -DgenerateBackupPoms=false
How to list your maven build version
public class MavenVersion {
private static final Logger LOG = Logger.getLogger(MavenVersion.class);
/**
* Print the version of all the maven library found in the class path.
*/
public static void printVersionInfo() {
for (MavenInfo mi : getVersionInfo().values()) {
String name = mi.artifactId.equals(mi.groupId) ? mi.artifactId : (mi.groupId + '/' + mi.artifactId);
LOG.info("Maven library " + name + " v" + mi.version + " on " + mi.buildDate + ", size=" + formatNumber("#,##0", mi.size));
}
}
private static final NumberFormat DEFAULT_DECIMAL_FORMAT = new DecimalFormat();
private static final ConcurrentMap<String, NumberFormat> NUMBER_FORMATS = new ConcurrentHashMap<String, NumberFormat>();
@NotNull
public static NumberFormat getNumberFormat(String format) {
NumberFormat df = format == null ? DEFAULT_DECIMAL_FORMAT : NUMBER_FORMATS.get(format);
if (df == null) {
assert format != null;
NUMBER_FORMATS.putIfAbsent(format, new DecimalFormat(format));
df = NUMBER_FORMATS.get(format);
}
return df;
}
@NotNull
public static String formatNumber(String format, @NotNull Number number) {
NumberFormat df = getNumberFormat(format);
synchronized (df) {
return df.format(number);
}
}
/**
* @return a Map of the maven version information for libraries found in the class path.
*/
public static Map<String, MavenInfo> getVersionInfo() {
Map<String, MavenInfo> ret = new HashMap<String, MavenInfo>();
String[] parts = System.getProperty("java.class.path").split(File.pathSeparator);
for (String part : parts) {
try {
File file = new File(part);
if (!file.isFile()) continue;
ZipFile zipFile = new JarFile(file);
for (final Enumeration<? extends ZipEntry> enumeration = zipFile.entries(); enumeration.hasMoreElements();)
{
ZipEntry zipEntry = enumeration.nextElement();
final String name = zipEntry.getName();
if (!name.endsWith("pom.properties"))
continue;
// only the first version is used. (except if classes have been deleted/renamed, urgh!)
if (ret.containsKey(name))
continue;
final InputStream in = zipFile.getInputStream(zipEntry);
ret.put(name, parsePomProperties(file.length(), in));
}
} catch (IOException e) {
e.printStackTrace();
}
}
if (ret.isEmpty())
LOG.warn("Unable to find Maven library info in class path= " + System.getProperty("java.class.path"));
return ret;
}
/**
* Class to hold the maven info for a library.
*/
public static class MavenInfo {
/**
* artifactId of build
*/
public final String artifactId;
/**
* groupId of build.
*/
public final String groupId;
/**
* version of build.
*/
public final String version;
/**
* date built.
*/
public final String buildDate;
/**
* size of the jar.
*/
public final long size;
MavenInfo(String artifactId, String groupId, String version, String buildDate, long size) {
this.artifactId = artifactId;
this.groupId = groupId;
this.version = version;
this.buildDate = buildDate;
this.size = size;
}
}
static MavenInfo parsePomProperties(long length, InputStream in) throws IOException {
final BufferedReader br = new BufferedReader(new InputStreamReader(in, Charset.forName("UTF-8")));
String version = "unknown";
String buildDate = "unknown";
String groupId = "unknown";
String artifactId = "unknown";
for (String line; (line = br.readLine()) != null;) {
// #.*:.*:.*
if (line.startsWith("#") && line.indexOf(':') != line.lastIndexOf(':')) {
buildDate = line.substring(1);
continue;
}
if (line.startsWith("version="))
version = line.substring(8);
if (line.startsWith("groupId="))
groupId = line.substring(8);
if (line.startsWith("artifactId="))
artifactId = line.substring(11);
}
return new MavenInfo(artifactId, groupId, version, buildDate, length);
}
}
how to kill surefire agents
ssh <box> kill -9 `ssh <box> ps auxww | grep surefire | grep java | awk '{print $2}'` &
how write a loop in a unix shell
for i in `seq 1 5`; do somecommand $i; done
how to export common test utility code
Create common test utils, and export this code by creating a test-jar, you have to use the following plugin, this jar will include the test scope
</build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>build-test-jar</id>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugins>
</build>
then in the jar that wished to use this code add import the jar with the following classifier
<dependency>
<groupId>your-groupId</groupId>
<artifactId>your-artifactId</artifactId>
<version>your-version</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>