001/*
002 * Copyright 2012-2015 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.cli.compiler.grape;
018
019import java.io.File;
020
021import org.eclipse.aether.DefaultRepositorySystemSession;
022import org.eclipse.aether.RepositorySystem;
023import org.eclipse.aether.repository.LocalRepository;
024import org.eclipse.aether.repository.LocalRepositoryManager;
025
026import org.springframework.util.StringUtils;
027
028/**
029 * Honours the configuration of {@code grape.root} by customizing the session's local
030 * repository location.
031 *
032 * @author Andy Wilkinson
033 * @since 1.2.5
034 */
035public class GrapeRootRepositorySystemSessionAutoConfiguration
036                implements RepositorySystemSessionAutoConfiguration {
037
038        @Override
039        public void apply(DefaultRepositorySystemSession session,
040                        RepositorySystem repositorySystem) {
041                String grapeRoot = System.getProperty("grape.root");
042                if (StringUtils.hasLength(grapeRoot)) {
043                        configureLocalRepository(session, repositorySystem, grapeRoot);
044                }
045        }
046
047        private void configureLocalRepository(DefaultRepositorySystemSession session,
048                        RepositorySystem repositorySystem, String grapeRoot) {
049                File repositoryDir = new File(grapeRoot, "repository");
050                LocalRepository localRepository = new LocalRepository(repositoryDir);
051                LocalRepositoryManager localRepositoryManager = repositorySystem
052                                .newLocalRepositoryManager(session, localRepository);
053                session.setLocalRepositoryManager(localRepositoryManager);
054        }
055
056}