The increasing penetration of renewable generation requires new tools to achieve a high matching between demand and renewable generation at building and community levels. Large commercial and public buildings with parking lots have a considerable potential to provide energy flexibility by controlling the charging of Electric Vehicles (EVs) and injecting part of the stored energy into the building, using Building-to-Vehicle (B2V) and Vehicle-to-Building (V2B) systems. However, EVs and buildings do not often belong to the same entity and in Portugal the existing regulation does not allow financial transactions between buildings and EVs as separate entities. Addressing this regulation hurdle requires innovative optimization methods for the implementation of B2V/V2B systems. Moreover, the new legislation regarding the self-consumption of renewable generation in Portugal enables the trade of renewable generation surplus between buildings and the establishment of renewable energy communities. This paper intends to address this issue and proposes a formulation to aggregate and manage the sharing of generation surplus between buildings, using EVs as a flexibility resource. The simulation results showcase the achieved increase in renewable self-consumption at building and community levels, as well as the reduction in electricity costs.