2011年11月5日土曜日

mavenでjavacのオプションを指定してみた


mavenのデフォルト設定では、アノテーションを使うとエラーになる。

デフォルト設定が、javacのオプションが-source 1.4と-target 1.4になっているのが原因。

pom.xmlで次のように-source 1.6と-target 1.6にすれば解決する。

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>
    </plugins>
  </build>
  ...
</project>

参考情報



-source release
Specifies the version of source code accepted. The following values for release are allowed:
1.3
The compiler does not support assertions, generics, or other language features introduced after JDK 1.3.
1.4
The compiler accepts code containing assertions, which were introduced in JDK 1.4.
1.5
The compiler accepts code containing generics and other language features introduced in JDK 5.
5
Synonym for 1.5.
1.6
This is the default value. No language changes were introduced in Java SE 6. However, encoding errors in source files are now reported as errors, instead of warnings, as previously.
6
Synonym for 1.6.

-target version
Generate class files that target a specified version of the VM. Class files will run on the specified target and on later versions, but not on earlier versions of the VM. Valid targets are 1.1 1.2 1.3 1.4 1.5 (also 5) and 1.6 (also 6).
The default for -target depends on the value of -source:

If -source is not specified, the value of -target is 1.6
If -source is 1.2, the value of -target is 1.4
If -source is 1.3, the value of -target is 1.4
For all other values of -source, the value of -target is the value of -source.

2011年7月10日日曜日

mavenで遊んでみた

いままでちゃんと使ってこなかったことを後悔するくらいmavenって便利かも。

mvnで依存関係解決すると、eclipseで-sources.jarとか-javadoc.jarもちゃんと解決してくれるのってすごく便利。

mvn packageとかmvn deployしたら、-sources.jarと-javadoc.jarも一緒に更新してくれるようにできるのって便利。

今日は、こんなpom.xmlを作って満足してるところ。

<project>
  ...

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <repositories>
    <repository>
      <id>local.file.system</id>
      <url>file:///c:/Users/Kei/Devel/Java/MavenRepos/</url>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
  </repositories>

  <distributionManagement>
    <repository>
      <id>deploy-repository</id>
      <name>deployRepository</name>
      <url>file:///c:/Users/Kei/Devel/Java/MavenRepos/</url>
    </repository>
  </distributionManagement>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <configuration>
          <excludeResources>true</excludeResources>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
              <goals>
                <goal>jar</goal>
              </goals>
          </execution>
        </executions>
      </plugin>
      
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-javadoc-plugin</artifactId>
        <configuration>
          <author>true</author>
          <source>1.6</source>
          <locale>en</locale>
          <!-- <locale>ja</locale> -->
          <showPackage>true</showPackage>
          <showPrivate>true</showPrivate>
          <encoding>utf-8</encoding>
          <charset>utf-8</charset>
          <decoding>utf-8</decoding>
        </configuration>
        <executions>
          <execution>
            <phase>package</phase>
              <goals>
                <goal>jar</goal>
              </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

androidのプロジェクトもmavenでビルドできたらいいのに。
android library projectの参照解決がしっかりできなくちゃダメ。
もしかして、それもできるのかな?知らないだけ?

2011年5月19日木曜日

AndroidのWebViewがhttpsでloadUrlすると真っ白になる件

Froyo以前のバージョンでは信頼できない証明書を受け入れるかどうかを選択する方法が提供されていないらしい。

しかも、警告も何もなしに読み込みを停止しちゃうので真っ白になる。

その解決方法がプライベートAPIを使ったものなのだけど、プライベートAPIの使い方が、「え?それでいいの?」って感じ。

Android WebView with https loadUrl shows blank/empty page

2011年4月11日月曜日

Apache HttpClientでJSONをgzipしてPOSTする


サーバ側はSinatraview (erb)で書いたらこんな感じ。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>post /</title>
</head>
<body>
<div>post /</div>
<div>Content-Type: <%= request.content_type%></div>
<div>Content-Encoding: <%= request.env["HTTP_CONTENT_ENCODING"]%></div>
<textarea cols="80" rows="20" readonly><%=
  if "gzip" == request.env["HTTP_CONTENT_ENCODING"]
    begin
      Zlib::Inflate.inflate(request.body.read)
    rescue => e
      e.pretty_inspect
    end
  else
    request.body.read
  end
%></textarea>
</body>
</html>

2011年4月8日金曜日

AndroidでSystem.outとSystem.errをLogに転送する

Androidでは、System.outとSystem.errがLogに転送されているけれど、
任意のプライオリティと任意のタグでLogに転送するコードを作ってみた。

ついでに、はじめてGistを使ってみた。

Gistで保存するコードの先頭には、コピーライトとか書かない方がいいのかもなぁ。



おもいっきりプライベートなAPI使ってるから良くない子に分類されるだろう。

2011年3月31日木曜日

JavaでJSONしたい

JavaでJSONを扱うためのライブラリはたくさんあるなぁ

どれを使ったらいいんだろう?

ちょっと古い記事だけど、A Review of 5 Java JSON Libraries が参考になりそう。

この記事で取り上げているのは次の5つ。

  1. org.json
  2. Jackson
  3. XStream
  4. JsonMarshaller
  5. JSON.simple

www.json.orgに18ものライブラリが掲載されていると書いてあるが、
その中からどういう基準で5つを選んだのか気になるところ。

それでも、これらの中のどれかを状況に合わせて選択すれば今を生きていくには十分だろう。

「とりあえずJSON使っておく?」
みたいな状況だったら JSON.simple を使えば良さそう。

ころで、AndroidのSDKにはorg.json.JSONObjectなどが用意されている。
パッケージ名から類推して org.json のコードが使われているのかと思っていたら間違いだった。

[platform/libcore.git] / json / src / main / java / org / json / JSONObject.java
ここのソースのコメントに、

// Note: this class was written without inspecting the non-free org.json sourcecode.

と書いてある。
オリジナルってことかな?

2011年3月1日火曜日

JavaでXPathを使ったことがあった

<?xml version="1.0" encoding="UTF-8"?>
<hello>
    <item>
        <world attr="value1"/>
    </item>
    <item>
        <world attr="value2"/>
    </item>
</hello>
import java.io.File;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XPathSample {
    public static void main(String[] args) throws Exception {
        XPathExpression xpath =
            XPathFactory.newInstance()
            .newXPath()
            .compile("//world[@attr='value2']");

        Document document =
            DocumentBuilderFactory.newInstance()
            .newDocumentBuilder()
            .parse(new File("hello.xml"));

        NodeList nodeList = (NodeList) xpath.evaluate(document, XPathConstants.NODESET);
        for (int i = 0; i < nodeList.getLength(); ++i) {
            Node node = nodeList.item(i);
            NamedNodeMap attributes = node.getAttributes();
            String value = attributes.getNamedItem("attr").getNodeValue();
            System.out.println(value);
        }
    }
}